2017-05-15 13:59:14 +00:00
|
|
|
/*---------------------------------------------------------------------------------------------
|
|
|
|
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
* Licensed under the MIT License. See License.txt in the project root for license information.
|
|
|
|
*--------------------------------------------------------------------------------------------*/
|
|
|
|
'use strict';
|
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
2018-10-12 18:24:15 +00:00
|
|
|
const es = require("event-stream");
|
|
|
|
const fs = require("fs");
|
|
|
|
const gulp = require("gulp");
|
|
|
|
const bom = require("gulp-bom");
|
|
|
|
const sourcemaps = require("gulp-sourcemaps");
|
|
|
|
const tsb = require("gulp-tsb");
|
|
|
|
const path = require("path");
|
|
|
|
const _ = require("underscore");
|
|
|
|
const monacodts = require("../monaco/api");
|
|
|
|
const nls = require("./nls");
|
|
|
|
const reporter_1 = require("./reporter");
|
|
|
|
const util = require("./util");
|
2019-02-05 21:21:05 +00:00
|
|
|
const fancyLog = require("fancy-log");
|
|
|
|
const ansiColors = require("ansi-colors");
|
2018-10-12 18:24:15 +00:00
|
|
|
const watch = require('./watch');
|
|
|
|
const reporter = reporter_1.createReporter();
|
2018-07-19 19:05:38 +00:00
|
|
|
function getTypeScriptCompilerOptions(src) {
|
2018-10-12 18:24:15 +00:00
|
|
|
const rootDir = path.join(__dirname, `../../${src}`);
|
|
|
|
const tsconfig = require(`../../${src}/tsconfig.json`);
|
|
|
|
let options;
|
2018-10-10 21:54:59 +00:00
|
|
|
if (tsconfig.extends) {
|
2018-10-12 18:24:15 +00:00
|
|
|
options = Object.assign({}, require(path.join(rootDir, tsconfig.extends)).compilerOptions, tsconfig.compilerOptions);
|
2018-10-10 21:54:59 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
options = tsconfig.compilerOptions;
|
|
|
|
}
|
2018-07-19 19:05:38 +00:00
|
|
|
options.verbose = false;
|
|
|
|
options.sourceMap = true;
|
|
|
|
if (process.env['VSCODE_NO_SOURCEMAP']) { // To be used by developers in a hurry
|
|
|
|
options.sourceMap = false;
|
|
|
|
}
|
|
|
|
options.rootDir = rootDir;
|
2018-10-02 08:48:21 +00:00
|
|
|
options.baseUrl = rootDir;
|
2018-07-19 19:05:38 +00:00
|
|
|
options.sourceRoot = util.toFileUri(rootDir);
|
|
|
|
options.newLine = /\r\n/.test(fs.readFileSync(__filename, 'utf8')) ? 'CRLF' : 'LF';
|
|
|
|
return options;
|
2017-12-15 15:07:15 +00:00
|
|
|
}
|
2018-07-19 19:05:38 +00:00
|
|
|
function createCompile(src, build, emitError) {
|
2018-10-12 18:24:15 +00:00
|
|
|
const opts = _.clone(getTypeScriptCompilerOptions(src));
|
2017-05-15 13:59:14 +00:00
|
|
|
opts.inlineSources = !!build;
|
|
|
|
opts.noFilesystemLookup = true;
|
2018-10-15 09:54:47 +00:00
|
|
|
const ts = tsb.create(opts, true, undefined, err => reporter(err.toString()));
|
2017-05-15 13:59:14 +00:00
|
|
|
return function (token) {
|
2018-10-12 18:24:15 +00:00
|
|
|
const utf8Filter = util.filter(data => /(\/|\\)test(\/|\\).*utf8/.test(data.path));
|
|
|
|
const tsFilter = util.filter(data => /\.ts$/.test(data.path));
|
|
|
|
const noDeclarationsFilter = util.filter(data => !(/\.d\.ts$/.test(data.path)));
|
|
|
|
const input = es.through();
|
|
|
|
const output = input
|
2017-05-15 13:59:14 +00:00
|
|
|
.pipe(utf8Filter)
|
|
|
|
.pipe(bom())
|
|
|
|
.pipe(utf8Filter.restore)
|
|
|
|
.pipe(tsFilter)
|
|
|
|
.pipe(util.loadSourcemaps())
|
|
|
|
.pipe(ts(token))
|
|
|
|
.pipe(noDeclarationsFilter)
|
|
|
|
.pipe(build ? nls() : es.through())
|
|
|
|
.pipe(noDeclarationsFilter.restore)
|
|
|
|
.pipe(sourcemaps.write('.', {
|
|
|
|
addComment: false,
|
|
|
|
includeContent: !!build,
|
2018-07-19 19:05:38 +00:00
|
|
|
sourceRoot: opts.sourceRoot
|
2017-05-15 13:59:14 +00:00
|
|
|
}))
|
|
|
|
.pipe(tsFilter.restore)
|
2018-10-03 00:19:03 +00:00
|
|
|
.pipe(reporter.end(!!emitError));
|
2017-05-15 13:59:14 +00:00
|
|
|
return es.duplex(input, output);
|
|
|
|
};
|
|
|
|
}
|
2018-10-12 18:24:15 +00:00
|
|
|
const typesDts = [
|
2018-10-02 21:53:54 +00:00
|
|
|
'node_modules/typescript/lib/*.d.ts',
|
|
|
|
'node_modules/@types/**/*.d.ts',
|
|
|
|
'!node_modules/@types/webpack/**/*',
|
|
|
|
'!node_modules/@types/uglify-js/**/*',
|
|
|
|
];
|
2018-07-19 19:05:38 +00:00
|
|
|
function compileTask(src, out, build) {
|
2019-02-12 19:43:28 +00:00
|
|
|
return function () {
|
2018-10-12 18:24:15 +00:00
|
|
|
const compile = createCompile(src, build, true);
|
|
|
|
const srcPipe = es.merge(gulp.src(`${src}/**`, { base: `${src}` }), gulp.src(typesDts));
|
2018-10-17 14:39:38 +00:00
|
|
|
let generator = new MonacoGenerator(false);
|
|
|
|
if (src === 'src') {
|
|
|
|
generator.execute();
|
|
|
|
}
|
2018-07-19 19:05:38 +00:00
|
|
|
return srcPipe
|
2018-10-17 14:39:38 +00:00
|
|
|
.pipe(generator.stream)
|
2017-05-15 13:59:14 +00:00
|
|
|
.pipe(compile())
|
2018-10-17 14:39:38 +00:00
|
|
|
.pipe(gulp.dest(out));
|
2017-05-15 13:59:14 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.compileTask = compileTask;
|
|
|
|
function watchTask(out, build) {
|
2019-02-12 19:43:28 +00:00
|
|
|
return function () {
|
2018-10-12 18:24:15 +00:00
|
|
|
const compile = createCompile('src', build);
|
|
|
|
const src = es.merge(gulp.src('src/**', { base: 'src' }), gulp.src(typesDts));
|
|
|
|
const watchSrc = watch('src/**', { base: 'src' });
|
2018-10-17 14:39:38 +00:00
|
|
|
let generator = new MonacoGenerator(true);
|
|
|
|
generator.execute();
|
2017-05-15 13:59:14 +00:00
|
|
|
return watchSrc
|
2018-10-17 14:39:38 +00:00
|
|
|
.pipe(generator.stream)
|
2017-05-15 13:59:14 +00:00
|
|
|
.pipe(util.incremental(compile, src, true))
|
2018-10-17 14:39:38 +00:00
|
|
|
.pipe(gulp.dest(out));
|
2017-05-15 13:59:14 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
exports.watchTask = watchTask;
|
2018-10-17 14:39:38 +00:00
|
|
|
const REPO_SRC_FOLDER = path.join(__dirname, '../../src');
|
|
|
|
class MonacoGenerator {
|
|
|
|
constructor(isWatch) {
|
2018-10-24 10:19:52 +00:00
|
|
|
this._executeSoonTimer = null;
|
2018-10-17 14:39:38 +00:00
|
|
|
this._isWatch = isWatch;
|
|
|
|
this.stream = es.through();
|
2018-10-24 13:56:37 +00:00
|
|
|
this._watchers = [];
|
|
|
|
this._watchedFiles = {};
|
|
|
|
let onWillReadFile = (moduleId, filePath) => {
|
|
|
|
if (!this._isWatch) {
|
|
|
|
return;
|
2018-10-17 14:39:38 +00:00
|
|
|
}
|
2018-10-24 13:56:37 +00:00
|
|
|
if (this._watchedFiles[filePath]) {
|
|
|
|
return;
|
2018-10-17 14:39:38 +00:00
|
|
|
}
|
2018-10-24 13:56:37 +00:00
|
|
|
this._watchedFiles[filePath] = true;
|
|
|
|
const watcher = fs.watch(filePath);
|
|
|
|
watcher.addListener('change', () => {
|
|
|
|
this._declarationResolver.invalidateCache(moduleId);
|
|
|
|
this._executeSoon();
|
2018-10-17 14:39:38 +00:00
|
|
|
});
|
2018-12-17 14:22:48 +00:00
|
|
|
watcher.addListener('error', (err) => {
|
|
|
|
console.error(`Encountered error while watching ${filePath}.`);
|
|
|
|
console.log(err);
|
|
|
|
delete this._watchedFiles[filePath];
|
|
|
|
for (let i = 0; i < this._watchers.length; i++) {
|
|
|
|
if (this._watchers[i] === watcher) {
|
|
|
|
this._watchers.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
watcher.close();
|
|
|
|
this._declarationResolver.invalidateCache(moduleId);
|
|
|
|
this._executeSoon();
|
|
|
|
});
|
2018-10-24 13:56:37 +00:00
|
|
|
this._watchers.push(watcher);
|
|
|
|
};
|
|
|
|
this._fsProvider = new class extends monacodts.FSProvider {
|
|
|
|
readFileSync(moduleId, filePath) {
|
|
|
|
onWillReadFile(moduleId, filePath);
|
|
|
|
return super.readFileSync(moduleId, filePath);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._declarationResolver = new monacodts.DeclarationResolver(this._fsProvider);
|
|
|
|
if (this._isWatch) {
|
2018-10-17 14:39:38 +00:00
|
|
|
const recipeWatcher = fs.watch(monacodts.RECIPE_PATH);
|
|
|
|
recipeWatcher.addListener('change', () => {
|
2018-10-24 10:19:52 +00:00
|
|
|
this._executeSoon();
|
2018-10-17 14:39:38 +00:00
|
|
|
});
|
|
|
|
this._watchers.push(recipeWatcher);
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-24 10:19:52 +00:00
|
|
|
_executeSoon() {
|
|
|
|
if (this._executeSoonTimer !== null) {
|
2018-10-24 13:56:37 +00:00
|
|
|
clearTimeout(this._executeSoonTimer);
|
|
|
|
this._executeSoonTimer = null;
|
2018-10-24 10:19:52 +00:00
|
|
|
}
|
|
|
|
this._executeSoonTimer = setTimeout(() => {
|
|
|
|
this._executeSoonTimer = null;
|
|
|
|
this.execute();
|
|
|
|
}, 20);
|
|
|
|
}
|
2018-10-17 14:39:38 +00:00
|
|
|
dispose() {
|
|
|
|
this._watchers.forEach(watcher => watcher.close());
|
|
|
|
}
|
|
|
|
_run() {
|
2018-10-24 13:56:37 +00:00
|
|
|
let r = monacodts.run3(this._declarationResolver);
|
2018-10-24 10:09:05 +00:00
|
|
|
if (!r && !this._isWatch) {
|
|
|
|
// The build must always be able to generate the monaco.d.ts
|
2018-10-24 13:56:37 +00:00
|
|
|
throw new Error(`monaco.d.ts generation error - Cannot continue`);
|
2018-10-24 10:09:05 +00:00
|
|
|
}
|
|
|
|
return r;
|
2018-10-17 14:39:38 +00:00
|
|
|
}
|
|
|
|
_log(message, ...rest) {
|
2019-02-05 21:21:05 +00:00
|
|
|
fancyLog(ansiColors.cyan('[monaco.d.ts]'), message, ...rest);
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|
2018-10-17 14:39:38 +00:00
|
|
|
execute() {
|
|
|
|
const startTime = Date.now();
|
|
|
|
const result = this._run();
|
|
|
|
if (!result) {
|
|
|
|
// nothing really changed
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (result.isTheSame) {
|
|
|
|
return;
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|
2018-10-17 14:39:38 +00:00
|
|
|
fs.writeFileSync(result.filePath, result.content);
|
2018-10-22 07:58:58 +00:00
|
|
|
fs.writeFileSync(path.join(REPO_SRC_FOLDER, 'vs/editor/common/standalone/standaloneEnums.ts'), result.enums);
|
2018-10-17 14:39:38 +00:00
|
|
|
this._log(`monaco.d.ts is changed - total time took ${Date.now() - startTime} ms`);
|
|
|
|
if (!this._isWatch) {
|
|
|
|
this.stream.emit('error', 'monaco.d.ts is no longer up to date. Please run gulp watch and commit the new file.');
|
|
|
|
}
|
|
|
|
}
|
2017-05-15 13:59:14 +00:00
|
|
|
}
|