Convert build scripts to JavaScript.

This commit is contained in:
Henning Dieterichs 2022-02-03 14:56:52 +01:00
parent 84665761ff
commit 4bf3b49c41
No known key found for this signature in database
GPG key ID: 771381EFFDB9EC06
14 changed files with 231 additions and 233 deletions

View file

@ -3,11 +3,9 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const glob = require('glob');
const { tsc, dts, buildESM, buildAMD } = require('../build/utils');
const { copyFile, removeDir } = require('../build/fs');
import glob from 'glob';
import { tsc, dts, buildESM, buildAMD } from './utils';
import { copyFile, removeDir } from './fs';
removeDir(`out`);

View file

@ -3,18 +3,14 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const fs = require('fs');
const path = require('path');
import * as fs from 'fs';
import * as path from 'path';
const REPO_ROOT = path.join(__dirname, '../');
const existingDirCache = new Set();
/**
* @param {string} dirname
*/
function ensureDir(dirname) {
export function ensureDir(dirname: string) {
/** @type {string[]} */
const dirs = [];
@ -32,15 +28,11 @@ function ensureDir(dirname) {
}
});
}
exports.ensureDir = ensureDir;
/**
* Copy a file.
*
* @param {string} _source
* @param {string} _destination
*/
function copyFile(_source, _destination) {
export function copyFile(_source: string, _destination: string) {
const source = path.join(REPO_ROOT, _source);
const destination = path.join(REPO_ROOT, _destination);
@ -49,15 +41,11 @@ function copyFile(_source, _destination) {
console.log(`Copied ${_source} to ${_destination}`);
}
exports.copyFile = copyFile;
/**
* Remove a directory and all its contents.
*
* @param {string} _dirPath
* @param {((filename:string)=>boolean)} [keep]
*/
function removeDir(_dirPath, keep) {
export function removeDir(_dirPath: string, keep?: (filename: string) => boolean) {
if (typeof keep === 'undefined') {
keep = () => false;
}
@ -95,4 +83,3 @@ function removeDir(_dirPath, keep) {
return keepsFiles;
}
}
exports.removeDir = removeDir;

View file

@ -3,12 +3,10 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const path = require('path');
const fs = require('fs');
const child_process = require('child_process');
const { REPO_ROOT } = require('./utils');
import path = require('path');
import fs = require('fs');
import child_process = require('child_process');
import { REPO_ROOT } from './utils';
const generatedNote = `//
// **NOTE**: Do not edit directly! This file is generated using \`npm run import-typescript\`

View file

@ -3,14 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const cp = require('child_process');
import glob = require('glob');
import path = require('path');
import fs = require('fs');
import cp = require('child_process');
const npm = process.platform === 'win32' ? 'npm.cmd' : 'npm';
const { REPO_ROOT } = require('../utils');
import { REPO_ROOT } from '../utils';
const files = glob.sync('**/package.json', {
cwd: REPO_ROOT,
@ -29,18 +27,16 @@ for (const file of files) {
}
function npmInstall(location) {
/** @type {'inherit'} */
const stdio = 'inherit';
const opts = {
env: process.env,
cwd: location,
stdio
};
const args = ['install'];
console.log(`Installing dependencies in ${location}...`);
console.log(`$ npm ${args.join(' ')}`);
const result = cp.spawnSync(npm, args, opts);
const result = cp.spawnSync(npm, args, {
env: process.env,
cwd: location,
stdio
});
if (result.error || result.status !== 0) {
process.exit(1);

View file

@ -3,12 +3,10 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const { REPO_ROOT } = require('../utils');
import glob from 'glob';
import path from 'path';
import fs from 'fs';
import { REPO_ROOT } from '../utils';
const files = glob.sync('**/package-lock.json', {
cwd: REPO_ROOT,

View file

@ -3,8 +3,8 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
const cp = require('child_process');
const path = require('path');
import cp = require('child_process');
import path = require('path');
function huskyInstall() {
console.log(`Installing husky hooks...`);

View file

@ -3,16 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
/** @typedef {import('../build/utils').IFile} IFile */
const path = require('path');
const fs = require('fs');
const { REPO_ROOT, readFiles, writeFiles } = require('../build/utils');
const { removeDir } = require('../build/fs');
const ts = require('typescript');
const { generateMetadata } = require('./releaseMetadata');
import path = require('path');
import fs = require('fs');
import { REPO_ROOT, readFiles, writeFiles, IFile } from '../build/utils';
import { removeDir } from '../build/fs';
import ts = require('typescript');
import { generateMetadata } from './releaseMetadata';
removeDir(`release`);
@ -89,11 +85,8 @@ function AMD_releaseOne(type) {
* - rename the AMD module 'vs/editor/editor.main' to 'vs/editor/edcore.main'
* - append monaco.contribution modules from plugins
* - append new AMD module 'vs/editor/editor.main' that stiches things together
*
* @param {'dev'|'min'} type
* @param {IFile[]} files
*/
function AMD_addPluginContribs(type, files) {
function AMD_addPluginContribs(type: 'dev' | 'min', files: IFile[]) {
for (const file of files) {
if (!/editor\.main\.js$/.test(file.path)) {
continue;
@ -223,9 +216,8 @@ function ESM_releasePlugins() {
/**
* Adds `.js` to all import statements.
* @param {IFile[]} files
*/
function ESM_addImportSuffix(files) {
function ESM_addImportSuffix(files: IFile[]) {
for (const file of files) {
if (!/\.js$/.test(file.path)) {
continue;
@ -254,9 +246,8 @@ function ESM_addImportSuffix(files) {
/**
* - Rename esm/vs/editor/editor.main.js to esm/vs/editor/edcore.main.js
* - Create esm/vs/editor/editor.main.js that that stiches things together
* @param {IFile[]} files
*/
function ESM_addPluginContribs(files) {
function ESM_addPluginContribs(files: IFile[]) {
for (const file of files) {
if (!/editor\.main\.js$/.test(file.path)) {
continue;
@ -340,10 +331,8 @@ function releaseDTS() {
/**
* Transforms a .d.ts which uses internal modules (namespaces) to one which is usable with external modules
* This function is duplicated in the `vscode` repo.
* @param {string} contents
* @returns string
*/
function toExternalDTS(contents) {
function toExternalDTS(contents: string): string {
let lines = contents.split(/\r\n|\r|\n/);
let killNextCloseCurlyBrace = false;
for (let i = 0; i < lines.length; i++) {
@ -387,10 +376,8 @@ function toExternalDTS(contents) {
/**
* Normalize line endings and ensure consistent 4 spaces indentation
* @param {string} contents
* @returns {string}
*/
function cleanFile(contents) {
function cleanFile(contents: string): string {
return contents
.split(/\r\n|\r|\n/)
.map(function (line) {

View file

@ -3,13 +3,11 @@
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const { REPO_ROOT } = require('./utils');
const { ensureDir } = require('./fs');
import glob = require('glob');
import path = require('path');
import fs = require('fs');
import { REPO_ROOT } from './utils';
import { ensureDir } from './fs';
const customFeatureLabels = {
'vs/editor/browser/controller/coreCommands': 'coreCommands',
@ -25,10 +23,7 @@ const customFeatureLabels = {
'vs/editor/standalone/browser/quickAccess/standaloneHelpQuickAccess': 'quickHelp'
};
/**
* @returns { Promise<{ label: string; entry: string; }[]> }
*/
function getBasicLanguages() {
function getBasicLanguages(): Promise<{ label: string; entry: string }[]> {
return new Promise((resolve, reject) => {
glob(
'./release/esm/vs/basic-languages/*/*.contribution.js',
@ -54,10 +49,7 @@ function getBasicLanguages() {
});
}
/**
* @returns { Promise<string[]> }
*/
function readAdvancedLanguages() {
function readAdvancedLanguages(): Promise<string[]> {
return new Promise((resolve, reject) => {
glob(
'./release/esm/vs/language/*/monaco.contribution.js',
@ -78,10 +70,9 @@ function readAdvancedLanguages() {
});
}
/**
* @returns { Promise<{ label: string; entry: string; worker: { id: string; entry: string; }; }[]> }
*/
function getAdvancedLanguages() {
function getAdvancedLanguages(): Promise<
{ label: string; entry: string; worker: { id: string; entry: string } }[]
> {
return readAdvancedLanguages().then((languages) => {
let result = [];
for (const lang of languages) {
@ -112,11 +103,11 @@ function getAdvancedLanguages() {
}
}
function generateMetadata() {
export function generateMetadata() {
return Promise.all([getBasicLanguages(), getAdvancedLanguages()]).then(
([basicLanguages, advancedLanguages]) => {
basicLanguages.sort(strcmp);
advancedLanguages.sort(strcmp);
basicLanguages.sort((a, b) => strcmp(a.entry, b.entry));
advancedLanguages.sort((a, b) => strcmp(a.entry, b.entry));
let i = 0,
len = basicLanguages.length;
@ -197,13 +188,8 @@ exports.languages = ${JSON.stringify(languages, null, ' ')};
}
);
}
exports.generateMetadata = generateMetadata;
/**
* @tyoe {string} a
* @tyoe {string} b
*/
function strcmp(a, b) {
function strcmp(a: string, b: string) {
if (a < b) {
return -1;
}
@ -213,10 +199,7 @@ function strcmp(a, b) {
return 0;
}
/**
* @returns {{label:string;entry:string|string[];}[]}
*/
function getFeatures() {
function getFeatures(): { label: string; entry: string | string[] }[] {
const skipImports = [
'vs/editor/browser/widget/codeEditorWidget',
'vs/editor/browser/widget/diffEditorWidget',
@ -228,8 +211,7 @@ function getFeatures() {
'vs/editor/contrib/gotoSymbol/documentSymbols'
];
/** @type {string[]} */
let features = [];
let features: string[] = [];
const files =
fs.readFileSync(path.join(REPO_ROOT, 'release/esm/vs/editor/edcore.main.js')).toString() +
fs.readFileSync(path.join(REPO_ROOT, 'release/esm/vs/editor/editor.all.js')).toString();
@ -243,8 +225,7 @@ function getFeatures() {
}
});
/** @type {{label:string;entry:any;}[]} */
let result = features.map((feature) => {
let result: { label: string; entry: any }[] = features.map((feature) => {
/** @type {string} */ let label;
if (customFeatureLabels[feature]) {
label = customFeatureLabels[feature];

View file

@ -3,14 +3,12 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
const fs = require('fs');
const path = require('path');
const http = require('http');
const yaserver = require('yaserver');
const { REPO_ROOT } = require('./utils');
const { ensureDir } = require('./fs');
import fs = require('fs');
import path = require('path');
import http = require('http');
import yaserver = require('yaserver');
import { REPO_ROOT } from './utils';
import { ensureDir } from './fs';
const WEBSITE_GENERATED_PATH = path.join(REPO_ROOT, 'website/playground/new-samples');
@ -157,11 +155,7 @@ function generateTestSamplesTask() {
);
}
/**
* @param {string} rootDir
* @param {number} port
*/
function createSimpleServer(rootDir, port) {
function createSimpleServer(rootDir: string, port: number) {
yaserver
.createServer({
rootDir: rootDir

7
build/tsconfig.json Normal file
View file

@ -0,0 +1,7 @@
{
"compilerOptions": {
"noEmit": true,
"esModuleInterop": true
},
"files": ["./**/*"]
}

View file

@ -3,26 +3,20 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
import * as fs from 'fs';
import * as path from 'path';
import * as cp from 'child_process';
import * as esbuild from 'esbuild';
import alias from 'esbuild-plugin-alias';
import * as glob from 'glob';
import { ensureDir } from './fs';
const fs = require('fs');
const path = require('path');
const cp = require('child_process');
const esbuild = require('esbuild');
/** @type {any} */
const alias = require('esbuild-plugin-alias');
const glob = require('glob');
const { ensureDir } = require('./fs');
const REPO_ROOT = path.join(__dirname, '../');
exports.REPO_ROOT = REPO_ROOT;
export const REPO_ROOT = path.join(__dirname, '../');
/**
* Launch the typescript compiler synchronously over a project.
*
* @param {string} _projectPath
*/
function tsc(_projectPath) {
export function tsc(_projectPath: string) {
const projectPath = path.join(REPO_ROOT, _projectPath);
console.log(`Launching compiler at ${_projectPath}...`);
const res = cp.spawnSync(
@ -35,14 +29,11 @@ function tsc(_projectPath) {
process.exit(res.status);
}
}
exports.tsc = tsc;
/**
* Launch prettier on a specific file.
*
* @param {string} _filePath
*/
function prettier(_filePath) {
export function prettier(_filePath: string) {
const filePath = path.join(REPO_ROOT, _filePath);
cp.spawnSync(
process.execPath,
@ -52,16 +43,11 @@ function prettier(_filePath) {
console.log(`Ran prettier over ${_filePath}`);
}
exports.prettier = prettier;
/**
* Transform an external .d.ts file to an internal .d.ts file
*
* @param {string} _source
* @param {string} _destination
* @param {string} namespace
*/
function dts(_source, _destination, namespace) {
export function dts(_source: string, _destination: string, namespace: string) {
const source = path.join(REPO_ROOT, _source);
const destination = path.join(REPO_ROOT, _destination);
@ -100,12 +86,8 @@ function dts(_source, _destination, namespace) {
prettier(_destination);
}
exports.dts = dts;
/**
* @param {import('esbuild').BuildOptions} options
*/
function build(options) {
export function build(options: import('esbuild').BuildOptions) {
esbuild.build(options).then((result) => {
if (result.errors.length > 0) {
console.error(result.errors);
@ -115,16 +97,8 @@ function build(options) {
}
});
}
exports.build = build;
/**
* @param {{
* base: string;
* entryPoints: string[];
* external: string[];
* }} options
*/
function buildESM(options) {
export function buildESM(options: { base: string; entryPoints: string[]; external: string[] }) {
build({
entryPoints: options.entryPoints,
bundle: true,
@ -146,26 +120,23 @@ function buildESM(options) {
]
});
}
exports.buildESM = buildESM;
/**
* @param {'dev'|'min'} type
* @param {{
* base: string;
* entryPoint: string;
* amdModuleId: string;
* amdDependencies?: string[];
* external?: string[];
* }} options
*/
function buildOneAMD(type, options) {
function buildOneAMD(
type: 'dev' | 'min',
options: {
base: string;
entryPoint: string;
amdModuleId: string;
amdDependencies?: string[];
external?: string[];
}
) {
if (!options.amdDependencies) {
options.amdDependencies = [];
}
options.amdDependencies.unshift('require');
/** @type {import('esbuild').BuildOptions} */
const opts = {
const opts: esbuild.BuildOptions = {
entryPoints: [options.entryPoint],
bundle: true,
target: 'esnext',
@ -198,20 +169,16 @@ function buildOneAMD(type, options) {
build(opts);
}
/**
* @param {{
* base: string;
* entryPoint: string;
* amdModuleId: string;
* amdDependencies?: string[];
* external?: string[];
* }} options
*/
function buildAMD(options) {
export function buildAMD(options: {
base: string;
entryPoint: string;
amdModuleId: string;
amdDependencies?: string[];
external?: string[];
}) {
buildOneAMD('dev', options);
buildOneAMD('min', options);
}
exports.buildAMD = buildAMD;
function getGitVersion() {
const git = path.join(REPO_ROOT, '.git');
@ -263,7 +230,7 @@ function getGitVersion() {
return refs[ref];
}
const bundledFileHeader = (() => {
export const bundledFileHeader = (() => {
const sha1 = getGitVersion();
const semver = require('../package.json').version;
const headerVersion = semver + '(' + sha1 + ')';
@ -280,16 +247,16 @@ const bundledFileHeader = (() => {
return BUNDLED_FILE_HEADER;
})();
exports.bundledFileHeader = bundledFileHeader;
/** @typedef {{ path:string; contents:Buffer;}} IFile */
export interface IFile {
path: string;
contents: Buffer;
}
/**
* @param {string} pattern
* @param {{ base:string; ignore?:string[]; dot?:boolean; }} options
* @returns {IFile[]}
*/
function readFiles(pattern, options) {
export function readFiles(
pattern: string,
options: { base: string; ignore?: string[]; dot?: boolean }
): IFile[] {
let files = glob.sync(pattern, { cwd: REPO_ROOT, ignore: options.ignore, dot: options.dot });
// remove dirs
files = files.filter((file) => {
@ -310,17 +277,11 @@ function readFiles(pattern, options) {
};
});
}
exports.readFiles = readFiles;
/**
* @param {IFile[]} files
* @param {string} dest
*/
function writeFiles(files, dest) {
export function writeFiles(files: IFile[], dest: string) {
for (const file of files) {
const fullPath = path.join(REPO_ROOT, dest, file.path);
ensureDir(path.dirname(fullPath));
fs.writeFileSync(fullPath, file.contents);
}
}
exports.writeFiles = writeFiles;

View file

@ -3,20 +3,15 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
//@ts-check
import glob = require('glob');
import path = require('path');
import fs = require('fs');
import cp = require('child_process');
import CleanCSS from 'clean-css';
import { REPO_ROOT, readFiles, writeFiles } from './utils';
import { removeDir } from './fs';
/** @typedef {import('../build/utils').IFile} IFile */
const glob = require('glob');
const path = require('path');
const fs = require('fs');
const cp = require('child_process');
const CleanCSS = require('clean-css');
const { REPO_ROOT, readFiles, writeFiles } = require('./utils');
const { removeDir } = require('./fs');
/** @type {string} */
const MONACO_EDITOR_VERSION = (() => {
const MONACO_EDITOR_VERSION: string = (() => {
const output = cp.execSync(`npm show monaco-editor version`).toString();
const version = output.split(/\r\n|\r|\n/g)[0];
if (!/\d+\.\d+\.\d+/.test(version)) {
@ -72,14 +67,12 @@ function checkSamples() {
}
}
/**
* @param {string} dataPath
* @param {string} contents
* @param {RegExp} regex
* @param {(match:string, fileContents:Buffer)=>string} callback
* @returns {string}
*/
function replaceWithRelativeResource(dataPath, contents, regex, callback) {
function replaceWithRelativeResource(
dataPath: string,
contents: string,
regex: RegExp,
callback: (match: string, fileContents: Buffer) => string
): string {
return contents.replace(regex, function (_, m0) {
const filePath = path.join(REPO_ROOT, 'website', path.dirname(dataPath), m0);
return callback(m0, fs.readFileSync(filePath));
@ -140,7 +133,7 @@ function generateWebsite() {
contents,
/<link data-inline="yes-please" href="([^"]+)".*/g,
function (m0, fileContents) {
const minifiedCSS = new CleanCSS().minify(fileContents.toString('utf8')).styles;
const minifiedCSS = (new CleanCSS() as any).minify(fileContents.toString('utf8')).styles;
return `<style>${minifiedCSS}</style>`;
}
);