vscode/build/hygiene.js

308 lines
7.8 KiB
JavaScript
Raw Permalink Normal View History

2020-09-22 11:12:35 +02:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
2020-09-22 14:37:04 +02:00
const filter = require('gulp-filter');
const es = require('event-stream');
const VinylFile = require('vinyl');
const vfs = require('vinyl-fs');
const path = require('path');
const fs = require('fs');
const pall = require('p-all');
const { all, copyrightFilter, unicodeFilter, indentationFilter, jsHygieneFilter, tsHygieneFilter } = require('./filters');
2020-09-22 11:12:35 +02:00
const copyrightHeaderLines = [
2020-09-22 14:37:04 +02:00
'/*---------------------------------------------------------------------------------------------',
' * Copyright (c) Microsoft Corporation. All rights reserved.',
' * Licensed under the MIT License. See License.txt in the project root for license information.',
' *--------------------------------------------------------------------------------------------*/',
2020-09-22 11:12:35 +02:00
];
2021-01-07 09:15:05 +01:00
function hygiene(some, linting = true) {
2020-12-22 19:55:56 +01:00
const gulpeslint = require('gulp-eslint');
const tsfmt = require('typescript-formatter');
2020-09-22 11:12:35 +02:00
let errorCount = 0;
const productJson = es.through(function (file) {
2020-09-22 14:37:04 +02:00
const product = JSON.parse(file.contents.toString('utf8'));
2020-09-22 11:12:35 +02:00
if (product.extensionsGallery) {
2020-09-22 14:37:04 +02:00
console.error(`product.json: Contains 'extensionsGallery'`);
2020-09-22 11:12:35 +02:00
errorCount++;
}
2020-09-22 14:37:04 +02:00
this.emit('data', file);
2020-09-22 11:12:35 +02:00
});
const unicode = es.through(function (file) {
2020-09-22 14:37:04 +02:00
const lines = file.contents.toString('utf8').split(/\r\n|\r|\n/);
2020-09-22 11:12:35 +02:00
file.__lines = lines;
let skipNext = false;
lines.forEach((line, i) => {
if (/allow-any-unicode-next-line/.test(line)) {
skipNext = true;
return;
}
if (skipNext) {
skipNext = false;
return;
}
// Please do not add symbols that resemble ASCII letters!
const m = /([^\t\n\r\x20-\x7E⊃⊇✔✓🎯⚠🛑🔴🚗🚙🚕🎉✨❗⇧⌥⌘×÷¦⋯…↑↓→→←↔⟷·•●◆▼⟪⟫┌└├⏎↩√φ]+)/g.exec(line);
if (m) {
console.error(
file.relative + `(${i + 1},${m.index + 1}): Unexpected unicode character: "${m[0]}". To suppress, use // allow-any-unicode-next-line`
);
errorCount++;
}
});
});
const indentation = es.through(function (file) {
const lines = file.__lines || file.contents.toString('utf8').split(/\r\n|\r|\n/);
file.__lines = lines;
2020-09-22 11:12:35 +02:00
lines.forEach((line, i) => {
if (/^\s*$/.test(line)) {
// empty or whitespace lines are OK
} else if (/^[\t]*[^\s]/.test(line)) {
// good indent
} else if (/^[\t]* \*/.test(line)) {
// block comment using an extra space
} else {
console.error(
2020-09-22 14:37:04 +02:00
file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation'
2020-09-22 11:12:35 +02:00
);
errorCount++;
}
});
2020-09-22 14:37:04 +02:00
this.emit('data', file);
2020-09-22 11:12:35 +02:00
});
const copyrights = es.through(function (file) {
const lines = file.__lines;
for (let i = 0; i < copyrightHeaderLines.length; i++) {
if (lines[i] !== copyrightHeaderLines[i]) {
2020-09-22 14:37:04 +02:00
console.error(file.relative + ': Missing or bad copyright statement');
2020-09-22 11:12:35 +02:00
errorCount++;
break;
}
}
2020-09-22 14:37:04 +02:00
this.emit('data', file);
2020-09-22 11:12:35 +02:00
});
const formatting = es.map(function (file, cb) {
tsfmt
2020-09-22 14:37:04 +02:00
.processString(file.path, file.contents.toString('utf8'), {
2020-09-22 11:12:35 +02:00
verify: false,
tsfmt: true,
// verbose: true,
// keep checkJS happy
editorconfig: undefined,
replace: undefined,
tsconfig: undefined,
tsconfigFile: undefined,
tsfmtFile: undefined,
vscode: undefined,
vscodeFile: undefined,
})
.then(
(result) => {
2020-09-22 14:37:04 +02:00
let original = result.src.replace(/\r\n/gm, '\n');
let formatted = result.dest.replace(/\r\n/gm, '\n');
2020-09-22 11:12:35 +02:00
if (original !== formatted) {
console.error(
2020-09-22 14:37:04 +02:00
`File not formatted. Run the 'Format Document' command to fix it:`,
2020-09-22 11:12:35 +02:00
file.relative
);
errorCount++;
}
cb(null, file);
},
(err) => {
cb(err);
}
);
});
let input;
2020-09-22 14:37:04 +02:00
if (Array.isArray(some) || typeof some === 'string' || !some) {
const options = { base: '.', follow: true, allowEmpty: true };
2020-09-22 11:12:35 +02:00
if (some) {
input = vfs.src(some, options).pipe(filter(all)); // split this up to not unnecessarily filter all a second time
} else {
input = vfs.src(all, options);
}
} else {
input = some;
}
2020-09-22 14:37:04 +02:00
const productJsonFilter = filter('product.json', { restore: true });
const unicodeFilterStream = filter(unicodeFilter, { restore: true });
2020-09-22 11:12:35 +02:00
const result = input
.pipe(filter((f) => !f.stat.isDirectory()))
.pipe(productJsonFilter)
2020-09-22 14:37:04 +02:00
.pipe(process.env['BUILD_SOURCEVERSION'] ? es.through() : productJson)
2020-09-22 11:12:35 +02:00
.pipe(productJsonFilter.restore)
.pipe(unicodeFilterStream)
.pipe(unicode)
.pipe(unicodeFilterStream.restore)
2020-09-22 11:12:35 +02:00
.pipe(filter(indentationFilter))
.pipe(indentation)
.pipe(filter(copyrightFilter))
.pipe(copyrights);
2021-01-07 09:15:05 +01:00
const streams = [
result.pipe(filter(tsHygieneFilter)).pipe(formatting)
];
if (linting) {
streams.push(
result
.pipe(filter([...jsHygieneFilter, ...tsHygieneFilter]))
.pipe(
gulpeslint({
configFile: '.eslintrc.json',
rulePaths: ['./build/lib/eslint'],
})
)
.pipe(gulpeslint.formatEach('compact'))
.pipe(
gulpeslint.results((results) => {
errorCount += results.warningCount;
errorCount += results.errorCount;
})
)
2020-09-22 11:12:35 +02:00
);
2021-01-07 09:15:05 +01:00
}
2020-09-22 11:12:35 +02:00
let count = 0;
2021-01-07 09:15:05 +01:00
return es.merge(...streams).pipe(
2020-09-22 11:12:35 +02:00
es.through(
function (data) {
count++;
2020-09-22 14:37:04 +02:00
if (process.env['TRAVIS'] && count % 10 === 0) {
process.stdout.write('.');
2020-09-22 11:12:35 +02:00
}
2020-09-22 14:37:04 +02:00
this.emit('data', data);
2020-09-22 11:12:35 +02:00
},
function () {
2020-09-22 14:37:04 +02:00
process.stdout.write('\n');
2020-09-22 11:12:35 +02:00
if (errorCount > 0) {
this.emit(
2020-09-22 14:37:04 +02:00
'error',
'Hygiene failed with ' +
errorCount +
` errors. Check 'build / gulpfile.hygiene.js'.`
2020-09-22 11:12:35 +02:00
);
} else {
2020-09-22 14:37:04 +02:00
this.emit('end');
2020-09-22 11:12:35 +02:00
}
}
)
);
}
module.exports.hygiene = hygiene;
function createGitIndexVinyls(paths) {
2020-09-22 14:37:04 +02:00
const cp = require('child_process');
2020-09-22 11:12:35 +02:00
const repositoryPath = process.cwd();
const fns = paths.map((relativePath) => () =>
new Promise((c, e) => {
const fullPath = path.join(repositoryPath, relativePath);
fs.stat(fullPath, (err, stat) => {
2020-09-22 14:37:04 +02:00
if (err && err.code === 'ENOENT') {
2020-09-22 11:12:35 +02:00
// ignore deletions
return c(null);
} else if (err) {
return e(err);
}
cp.exec(
2021-01-25 17:46:09 +01:00
process.platform === 'win32' ? `git show :${relativePath}` : `git show ':${relativePath}'`,
2020-09-22 14:37:04 +02:00
{ maxBuffer: 2000 * 1024, encoding: 'buffer' },
2020-09-22 11:12:35 +02:00
(err, out) => {
if (err) {
return e(err);
}
c(
new VinylFile({
path: fullPath,
base: repositoryPath,
contents: out,
stat,
})
);
}
);
});
})
);
return pall(fns, { concurrency: 4 }).then((r) => r.filter((p) => !!p));
}
// this allows us to run hygiene as a git pre-commit hook
if (require.main === module) {
2020-09-22 14:37:04 +02:00
const cp = require('child_process');
2020-09-22 11:12:35 +02:00
2020-09-22 14:37:04 +02:00
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
2020-09-22 11:12:35 +02:00
process.exit(1);
});
if (process.argv.length > 2) {
2020-09-22 14:37:04 +02:00
hygiene(process.argv.slice(2)).on('error', (err) => {
2020-09-22 11:12:35 +02:00
console.error();
console.error(err);
process.exit(1);
});
} else {
cp.exec(
2020-09-22 14:37:04 +02:00
'git diff --cached --name-only',
2020-09-22 11:12:35 +02:00
{ maxBuffer: 2000 * 1024 },
(err, out) => {
if (err) {
console.error();
console.error(err);
process.exit(1);
}
const some = out.split(/\r?\n/).filter((l) => !!l);
if (some.length > 0) {
2020-09-22 14:37:04 +02:00
console.log('Reading git index versions...');
2020-09-22 11:12:35 +02:00
createGitIndexVinyls(some)
.then(
(vinyls) =>
new Promise((c, e) =>
hygiene(es.readArray(vinyls))
2020-09-22 14:37:04 +02:00
.on('end', () => c())
.on('error', e)
2020-09-22 11:12:35 +02:00
)
)
.catch((err) => {
console.error();
console.error(err);
process.exit(1);
});
}
}
);
}
}