vscode/build/hygiene.js

314 lines
8.3 KiB
JavaScript
Raw Normal View History

2020-09-22 17:12:35 +08: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 20:37:04 +08: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, tsFormattingFilter, eslintFilter, stylelintFilter } = require('./filters');
2020-09-22 17:12:35 +08:00
const copyrightHeaderLines = [
2020-09-22 20:37:04 +08: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 17:12:35 +08:00
];
2021-01-07 16:15:05 +08:00
function hygiene(some, linting = true) {
const eslint = require('./gulp-eslint');
const gulpstylelint = require('./stylelint');
const formatter = require('./lib/formatter');
2020-12-23 02:55:56 +08:00
2020-09-22 17:12:35 +08:00
let errorCount = 0;
const productJson = es.through(function (file) {
2020-09-22 20:37:04 +08:00
const product = JSON.parse(file.contents.toString('utf8'));
2020-09-22 17:12:35 +08:00
if (product.extensionsGallery) {
2020-09-22 20:37:04 +08:00
console.error(`product.json: Contains 'extensionsGallery'`);
2020-09-22 17:12:35 +08:00
errorCount++;
}
2020-09-22 20:37:04 +08:00
this.emit('data', file);
2020-09-22 17:12:35 +08:00
});
const unicode = es.through(function (file) {
2020-09-22 20:37:04 +08:00
const lines = file.contents.toString('utf8').split(/\r\n|\r|\n/);
2020-09-22 17:12:35 +08:00
file.__lines = lines;
const allowInComments = lines.some(line => /allow-any-unicode-comment-file/.test(line));
let skipNext = false;
lines.forEach((line, i) => {
if (/allow-any-unicode-next-line/.test(line)) {
skipNext = true;
return;
}
if (skipNext) {
skipNext = false;
return;
}
// If unicode is allowed in comments, trim the comment from the line
if (allowInComments) {
if (line.match(/\s+(\*)/)) { // Naive multi-line comment check
line = '';
} else {
const index = line.indexOf('\/\/');
line = index === -1 ? line : line.substring(0, index);
}
}
// Please do not add symbols that resemble ASCII letters!
2024-10-03 08:22:33 +08:00
// eslint-disable-next-line no-misleading-character-class
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]}" (charCode: ${m[0].charCodeAt(0)}). To suppress, use // allow-any-unicode-next-line`
);
errorCount++;
}
});
this.emit('data', file);
});
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 17:12:35 +08: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 20:37:04 +08:00
file.relative + '(' + (i + 1) + ',1): Bad whitespace indentation'
2020-09-22 17:12:35 +08:00
);
errorCount++;
}
});
2020-09-22 20:37:04 +08:00
this.emit('data', file);
2020-09-22 17:12:35 +08: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 20:37:04 +08:00
console.error(file.relative + ': Missing or bad copyright statement');
2020-09-22 17:12:35 +08:00
errorCount++;
break;
}
}
2020-09-22 20:37:04 +08:00
this.emit('data', file);
2020-09-22 17:12:35 +08:00
});
const formatting = es.map(function (file, cb) {
try {
const rawInput = file.contents.toString('utf8');
const rawOutput = formatter.format(file.path, rawInput);
const original = rawInput.replace(/\r\n/gm, '\n');
const formatted = rawOutput.replace(/\r\n/gm, '\n');
if (original !== formatted) {
console.error(
`File not formatted. Run the 'Format Document' command to fix it:`,
file.relative
);
errorCount++;
}
cb(null, file);
} catch (err) {
cb(err);
}
2020-09-22 17:12:35 +08:00
});
let input;
2020-09-22 20:37:04 +08:00
if (Array.isArray(some) || typeof some === 'string' || !some) {
const options = { base: '.', follow: true, allowEmpty: true };
2020-09-22 17:12:35 +08: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 20:37:04 +08:00
const productJsonFilter = filter('product.json', { restore: true });
const snapshotFilter = filter(['**', '!**/*.snap', '!**/*.snap.actual']);
feat: switch to npm as default package manager (#226927) * feat: move from yarn to npm * chore: skip yarn.lock files * fix: playwright download * chore: fix compile and hygiene * chore: bump vsce@2.17.0 Refs https://github.com/microsoft/vscode-vsce/commit/8b49e9dfdf909ad3af2b9ec9c825f5b501f6d75e * test: update results for bat and sh colorizer tests * fix: add missing lock files for windows * fix: switch to legacy-peer-deps * chore: update markdown-it@14.1.0 Refs https://github.com/markdown-it/markdown-it/commit/737c95a12976357df99652e4b51d831cac4a75aa esbuild step in extensions-ci-pr was previously using markdown-it from root which had userland punycode and was able to compile successfully. * ci: increase pr timeout for windows integration tests * chore: fix product build * build: ignore extension dev dependency for rcedit * build: fix working directory inside container * build: fix dependency generation * npm: update dependencies * ci: use global npmrc * ci: update cache * ci: setup global npmrc for private npm auth * build: fix extension bundling * chore: sync npm dependencies * ci: debug env variables for container * ci: fix win32 cli pipeline * build: fix npmrc config usage for build/ and remote/ dirs * fix: windows build * fix: container builds * fix: markdown-language-features tests and bundling ``` [03:58:22] Error: Command failed: /Users/demohan/.nvm/versions/node/v20.15.1/bin/node /Users/demohan/github/vscode/extensions/markdown-language-features/esbuild-notebook.js --outputRoot /Users/demohan/github/vscode/.build/extensions/markdown-language-features ✘ [ERROR] Could not resolve "punycode" extensions/markdown-language-features/node_modules/markdown-it/lib/index.js:14:27: 14 │ var punycode = require('punycode'); ╵ ~~~~~~~~~~ The package "punycode" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. ``` Adds userland package based on https://github.com/markdown-it/markdown-it/commit/beed9aee2c1b44819afc25d485e86a6c699b6ef0 * fix: container builds for distro * chore: update yarn occurrences * fixup! chore: bump vsce@2.17.0 Uses the closest version to `main` branch that does not include https://github.com/microsoft/vscode-vsce/commit/d3cc84cdec99b8e9f24be63d1a2a54abe908f68b while still having the fix https://github.com/microsoft/vscode-vsce/commit/8b49e9dfdf909ad3af2b9ec9c825f5b501f6d75e * chore: sync npm dependencies * chore: sync npm dependencies * chore: sync npm dependencies * chore: throw error when yarn is used for installation * chore: add review feedback * chore: switch exec => run where needed * chore: npm sync dependencies * fix: markdown-language-features bundling ``` ✘ [ERROR] Could not resolve "punycode" extensions/markdown-language-features/node_modules/markdown-it/lib/index.js:14:27: 14 │ var punycode = require('punycode'); ╵ ~~~~~~~~~~ The package "punycode" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. ``` Adds missing userland package based on markdown-it/markdown-it@beed9ae, can be removed once we update markdown-it >= 14.1.0 * ci: rename no-yarn-lock-changes.yml * chore: sync npm dependencies * ci: restore no-yarn-lock-changes.yml We can disable it in a separate PR to keep the required checks happy and also need workflow edit perms. * chore: sync npm dependencies * ci: rebuild cache * ci: fix no-package-lock-changes.yml * chore: bump distro * chore: rm yarn.lock files * chore: rm yarn.lock files without dependencies * chore: add vscode-selfhost-import-aid to postinstall dirs * chore: bump distro
2024-09-06 21:18:02 +08:00
const yarnLockFilter = filter(['**', '!**/yarn.lock']);
const unicodeFilterStream = filter(unicodeFilter, { restore: true });
2020-09-22 17:12:35 +08:00
const result = input
.pipe(filter((f) => !f.stat.isDirectory()))
.pipe(snapshotFilter)
feat: switch to npm as default package manager (#226927) * feat: move from yarn to npm * chore: skip yarn.lock files * fix: playwright download * chore: fix compile and hygiene * chore: bump vsce@2.17.0 Refs https://github.com/microsoft/vscode-vsce/commit/8b49e9dfdf909ad3af2b9ec9c825f5b501f6d75e * test: update results for bat and sh colorizer tests * fix: add missing lock files for windows * fix: switch to legacy-peer-deps * chore: update markdown-it@14.1.0 Refs https://github.com/markdown-it/markdown-it/commit/737c95a12976357df99652e4b51d831cac4a75aa esbuild step in extensions-ci-pr was previously using markdown-it from root which had userland punycode and was able to compile successfully. * ci: increase pr timeout for windows integration tests * chore: fix product build * build: ignore extension dev dependency for rcedit * build: fix working directory inside container * build: fix dependency generation * npm: update dependencies * ci: use global npmrc * ci: update cache * ci: setup global npmrc for private npm auth * build: fix extension bundling * chore: sync npm dependencies * ci: debug env variables for container * ci: fix win32 cli pipeline * build: fix npmrc config usage for build/ and remote/ dirs * fix: windows build * fix: container builds * fix: markdown-language-features tests and bundling ``` [03:58:22] Error: Command failed: /Users/demohan/.nvm/versions/node/v20.15.1/bin/node /Users/demohan/github/vscode/extensions/markdown-language-features/esbuild-notebook.js --outputRoot /Users/demohan/github/vscode/.build/extensions/markdown-language-features ✘ [ERROR] Could not resolve "punycode" extensions/markdown-language-features/node_modules/markdown-it/lib/index.js:14:27: 14 │ var punycode = require('punycode'); ╵ ~~~~~~~~~~ The package "punycode" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. ``` Adds userland package based on https://github.com/markdown-it/markdown-it/commit/beed9aee2c1b44819afc25d485e86a6c699b6ef0 * fix: container builds for distro * chore: update yarn occurrences * fixup! chore: bump vsce@2.17.0 Uses the closest version to `main` branch that does not include https://github.com/microsoft/vscode-vsce/commit/d3cc84cdec99b8e9f24be63d1a2a54abe908f68b while still having the fix https://github.com/microsoft/vscode-vsce/commit/8b49e9dfdf909ad3af2b9ec9c825f5b501f6d75e * chore: sync npm dependencies * chore: sync npm dependencies * chore: sync npm dependencies * chore: throw error when yarn is used for installation * chore: add review feedback * chore: switch exec => run where needed * chore: npm sync dependencies * fix: markdown-language-features bundling ``` ✘ [ERROR] Could not resolve "punycode" extensions/markdown-language-features/node_modules/markdown-it/lib/index.js:14:27: 14 │ var punycode = require('punycode'); ╵ ~~~~~~~~~~ The package "punycode" wasn't found on the file system but is built into node. Are you trying to bundle for node? You can use "platform: 'node'" to do that, which will remove this error. ``` Adds missing userland package based on markdown-it/markdown-it@beed9ae, can be removed once we update markdown-it >= 14.1.0 * ci: rename no-yarn-lock-changes.yml * chore: sync npm dependencies * ci: restore no-yarn-lock-changes.yml We can disable it in a separate PR to keep the required checks happy and also need workflow edit perms. * chore: sync npm dependencies * ci: rebuild cache * ci: fix no-package-lock-changes.yml * chore: bump distro * chore: rm yarn.lock files * chore: rm yarn.lock files without dependencies * chore: add vscode-selfhost-import-aid to postinstall dirs * chore: bump distro
2024-09-06 21:18:02 +08:00
.pipe(yarnLockFilter)
2020-09-22 17:12:35 +08:00
.pipe(productJsonFilter)
2020-09-22 20:37:04 +08:00
.pipe(process.env['BUILD_SOURCEVERSION'] ? es.through() : productJson)
2020-09-22 17:12:35 +08:00
.pipe(productJsonFilter.restore)
.pipe(unicodeFilterStream)
.pipe(unicode)
.pipe(unicodeFilterStream.restore)
2020-09-22 17:12:35 +08:00
.pipe(filter(indentationFilter))
.pipe(indentation)
.pipe(filter(copyrightFilter))
.pipe(copyrights);
2021-01-07 16:15:05 +08:00
const streams = [
2022-02-04 00:12:23 +08:00
result.pipe(filter(tsFormattingFilter)).pipe(formatting)
2021-01-07 16:15:05 +08:00
];
if (linting) {
streams.push(
result
2022-02-04 00:12:23 +08:00
.pipe(filter(eslintFilter))
2021-01-07 16:15:05 +08:00
.pipe(
eslint((results) => {
2021-01-07 16:15:05 +08:00
errorCount += results.warningCount;
errorCount += results.errorCount;
})
)
2020-09-22 17:12:35 +08:00
);
streams.push(
result.pipe(filter(stylelintFilter)).pipe(gulpstylelint(((message, isError) => {
if (isError) {
console.error(message);
errorCount++;
} else {
console.warn(message);
}
})))
);
2021-01-07 16:15:05 +08:00
}
2020-09-22 17:12:35 +08:00
let count = 0;
2021-01-07 16:15:05 +08:00
return es.merge(...streams).pipe(
2020-09-22 17:12:35 +08:00
es.through(
function (data) {
count++;
2020-09-22 20:37:04 +08:00
if (process.env['TRAVIS'] && count % 10 === 0) {
process.stdout.write('.');
2020-09-22 17:12:35 +08:00
}
2020-09-22 20:37:04 +08:00
this.emit('data', data);
2020-09-22 17:12:35 +08:00
},
function () {
2020-09-22 20:37:04 +08:00
process.stdout.write('\n');
2020-09-22 17:12:35 +08:00
if (errorCount > 0) {
this.emit(
2020-09-22 20:37:04 +08:00
'error',
'Hygiene failed with ' +
errorCount +
` errors. Check 'build / gulpfile.hygiene.js'.`
2020-09-22 17:12:35 +08:00
);
} else {
2020-09-22 20:37:04 +08:00
this.emit('end');
2020-09-22 17:12:35 +08:00
}
}
)
);
}
module.exports.hygiene = hygiene;
function createGitIndexVinyls(paths) {
2020-09-22 20:37:04 +08:00
const cp = require('child_process');
2020-09-22 17:12:35 +08: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 20:37:04 +08:00
if (err && err.code === 'ENOENT') {
2020-09-22 17:12:35 +08:00
// ignore deletions
return c(null);
} else if (err) {
return e(err);
}
cp.exec(
2021-01-26 00:46:09 +08:00
process.platform === 'win32' ? `git show :${relativePath}` : `git show ':${relativePath}'`,
{ maxBuffer: stat.size, encoding: 'buffer' },
2020-09-22 17:12:35 +08: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 20:37:04 +08:00
const cp = require('child_process');
2020-09-22 17:12:35 +08:00
2020-09-22 20:37:04 +08:00
process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
2020-09-22 17:12:35 +08:00
process.exit(1);
});
if (process.argv.length > 2) {
2020-09-22 20:37:04 +08:00
hygiene(process.argv.slice(2)).on('error', (err) => {
2020-09-22 17:12:35 +08:00
console.error();
console.error(err);
process.exit(1);
});
} else {
cp.exec(
2020-09-22 20:37:04 +08:00
'git diff --cached --name-only',
2020-09-22 17:12:35 +08: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 20:37:04 +08:00
console.log('Reading git index versions...');
2020-09-22 17:12:35 +08:00
createGitIndexVinyls(some)
.then(
(vinyls) =>
new Promise((c, e) =>
hygiene(es.readArray(vinyls).pipe(filter(all)))
2020-09-22 20:37:04 +08:00
.on('end', () => c())
.on('error', e)
2020-09-22 17:12:35 +08:00
)
)
.catch((err) => {
console.error();
console.error(err);
process.exit(1);
});
}
}
);
}
}