From 160d0e06f8ff07c9fc7d559c89736cb263d6fd99 Mon Sep 17 00:00:00 2001 From: whosafe Date: Fri, 15 Dec 2023 21:29:48 +0800 Subject: [PATCH] Filter data from other platforms (#912) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Filter data from other platforms When the parameter target has a value, all files will be filtered based on the target value. All file directories are in the target, but directories with different values from the target will be excluded from this packaging. For example: ui |-style |- linux-x64 | |- default.css |- win32-x64 |- default.css When using vsce package - t win32-x64 for packaging, the linux-x64 directory will not be packaged into the program。 当参数 target 有值时,会根据target值过滤所有文件,所有文件目录在Target中,但时和target值不相同的目录,将会再本次打包中排除 。 如果: ui |-style |- linux-x64 | |- default.css |- win32-x64 |- default.css 在使用vsce package -t win32-x64 进行打包时,linux-x64目录将不会打包到程序里 * Add target and ignoreOtherTargetFolders options --------- Co-authored-by: 曾洪亮 Co-authored-by: João Moreno --- src/main.ts | 6 ++ src/package.ts | 66 ++++++++++++------- src/publish.ts | 1 + .../fixtures/target/darwin-arm64/file.txt | 0 .../target/deep/darwin-arm64/file.txt | 0 src/test/fixtures/target/deep/file.txt | 0 .../fixtures/target/deep/linux-x64/file.txt | 0 src/test/fixtures/target/deep/random/file.txt | 0 src/test/fixtures/target/deep/web/file.txt | 0 src/test/fixtures/target/file.txt | 0 src/test/fixtures/target/linux-x64/file.txt | 0 src/test/fixtures/target/package.json | 6 ++ src/test/fixtures/target/random/file.txt | 0 src/test/fixtures/target/web/file.txt | 0 src/test/package.test.ts | 42 ++++++++++++ 15 files changed, 97 insertions(+), 24 deletions(-) create mode 100644 src/test/fixtures/target/darwin-arm64/file.txt create mode 100644 src/test/fixtures/target/deep/darwin-arm64/file.txt create mode 100644 src/test/fixtures/target/deep/file.txt create mode 100644 src/test/fixtures/target/deep/linux-x64/file.txt create mode 100644 src/test/fixtures/target/deep/random/file.txt create mode 100644 src/test/fixtures/target/deep/web/file.txt create mode 100644 src/test/fixtures/target/file.txt create mode 100644 src/test/fixtures/target/linux-x64/file.txt create mode 100644 src/test/fixtures/target/package.json create mode 100644 src/test/fixtures/target/random/file.txt create mode 100644 src/test/fixtures/target/web/file.txt diff --git a/src/main.ts b/src/main.ts index 1b75da41..bd04dfb7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -85,6 +85,7 @@ module.exports = function (argv: string[]): void { .description('Packages an extension') .option('-o, --out ', 'Output .vsix extension file to location (defaults to -.vsix)') .option('-t, --target ', `Target architecture. Valid targets: ${ValidTargets}`) + .option('--ignore-other-target-folders', `Ignore other target folders. Valid only when --target is provided.`) .option('-m, --message ', 'Commit message used when calling `npm version`.') .option( '--no-git-tag-version', @@ -120,6 +121,7 @@ module.exports = function (argv: string[]): void { { out, target, + ignoreOtherTargetFolders, message, gitTagVersion, updatePackageJson, @@ -144,6 +146,7 @@ module.exports = function (argv: string[]): void { packagePath: out, version, target, + ignoreOtherTargetFolders, commitMessage: message, gitTagVersion, updatePackageJson, @@ -174,6 +177,7 @@ module.exports = function (argv: string[]): void { process.env['VSCE_PAT'] ) .option('-t, --target ', `Target architectures. Valid targets: ${ValidTargets}`) + .option('--ignore-other-target-folders', `Ignore other target folders. Valid only when --target is provided.`) .option('-m, --message ', 'Commit message used when calling `npm version`.') .option( '--no-git-tag-version', @@ -211,6 +215,7 @@ module.exports = function (argv: string[]): void { { pat, target, + ignoreOtherTargetFolders, message, gitTagVersion, updatePackageJson, @@ -237,6 +242,7 @@ module.exports = function (argv: string[]): void { pat, version, targets: target, + ignoreOtherTargetFolders, commitMessage: message, gitTagVersion, updatePackageJson, diff --git a/src/package.ts b/src/package.ts index 93a893dd..eeee356a 100644 --- a/src/package.ts +++ b/src/package.ts @@ -86,6 +86,17 @@ export interface IPackageOptions { * https://code.visualstudio.com/api/working-with-extensions/publishing-extension#platformspecific-extensions */ readonly target?: string; + + /** + * Ignore all files inside folders named as other targets. Only relevant when + * `target` is set. For example, if `target` is `linux-x64` and there are + * folders named `win32-x64`, `darwin-arm64` or `web`, the files inside + * those folders will be ignored. + * + * @default false + */ + readonly ignoreOtherTargetFolders?: boolean; + readonly commitMessage?: string; readonly gitTagVersion?: boolean; readonly updatePackageJson?: boolean; @@ -1538,7 +1549,7 @@ async function collectAllFiles( const promises = deps.map(dep => promisify(glob)('**', { cwd: dep.src, nodir: true, dot: true, ignore: 'node_modules/**' }).then(files => files.map(f => ({ - src: path.relative(cwd, path.join(dep.src, f.replace(/\\/g, '/'))), + src: path.relative(cwd, path.join(dep.src, f)).replace(/\\/g, '/'), dest: path.join(dep.dest, f).replace(/\\/g, '/') })) ) @@ -1547,16 +1558,39 @@ async function collectAllFiles( return Promise.all(promises).then(util.flatten); } +function getDependenciesOption(options: IPackageOptions): 'npm' | 'yarn' | 'none' | undefined { + if (options.dependencies === false) { + return 'none'; + } + + switch (options.useYarn) { + case true: + return 'yarn'; + case false: + return 'npm'; + default: + return undefined; + } +} + function collectFiles( cwd: string, manifest: Manifest, - dependencies: 'npm' | 'yarn' | 'none' | undefined, - dependencyEntryPoints?: string[], - ignoreFile?: string + options: IPackageOptions ): Promise { - return collectAllFiles(cwd, manifest, dependencies, dependencyEntryPoints).then(files => { + const packagedDependencies = options.dependencyEntryPoints || undefined; + const ignoreFile = options.ignoreFile || undefined; + const target = options.target || undefined; + + return collectAllFiles(cwd, manifest, getDependenciesOption(options), packagedDependencies).then(files => { files = files.filter(f => !/\r$/m.test(f.src)); + // Filter data from other platforms + if (target && options.ignoreOtherTargetFolders) { + const regex = new RegExp(`(^|/)(${Array.from(Targets, v => v).filter(v => v !== target).join('|')})/`); + files = files.filter(f => !regex.test(f.src)) + } + return ( fs.promises .readFile(ignoreFile ? ignoreFile : path.join(cwd, '.vscodeignore'), 'utf8') @@ -1646,28 +1680,11 @@ export function createDefaultProcessors(manifest: Manifest, options: IPackageOpt ]; } -function getDependenciesOption(options: IListFilesOptions): 'npm' | 'yarn' | 'none' | undefined { - if (options.dependencies === false) { - return 'none'; - } - - switch (options.useYarn) { - case true: - return 'yarn'; - case false: - return 'npm'; - default: - return undefined; - } -} - export function collect(manifest: Manifest, options: IPackageOptions = {}): Promise { const cwd = options.cwd || process.cwd(); - const packagedDependencies = options.dependencyEntryPoints || undefined; - const ignoreFile = options.ignoreFile || undefined; const processors = createDefaultProcessors(manifest, options); - return collectFiles(cwd, manifest, getDependenciesOption(options), packagedDependencies, ignoreFile).then(fileNames => { + return collectFiles(cwd, manifest, options).then(fileNames => { const files = fileNames.map(f => ({ path: `extension/${f.dest}`, localPath: path.join(cwd, f.src) })); return processFiles(processors, files); @@ -1819,7 +1836,8 @@ export async function listFiles(options: IListFilesOptions = {}): Promise f.src); + const files = await collectFiles(cwd, manifest, options); + return files.map(f => f.src); } /** diff --git a/src/publish.ts b/src/publish.ts index 1a386471..84cf6469 100644 --- a/src/publish.ts +++ b/src/publish.ts @@ -20,6 +20,7 @@ export interface IPublishOptions { readonly packagePath?: string[]; readonly version?: string; readonly targets?: string[]; + readonly ignoreOtherTargetFolders?: boolean; readonly commitMessage?: string; readonly gitTagVersion?: boolean; readonly updatePackageJson?: boolean; diff --git a/src/test/fixtures/target/darwin-arm64/file.txt b/src/test/fixtures/target/darwin-arm64/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/deep/darwin-arm64/file.txt b/src/test/fixtures/target/deep/darwin-arm64/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/deep/file.txt b/src/test/fixtures/target/deep/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/deep/linux-x64/file.txt b/src/test/fixtures/target/deep/linux-x64/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/deep/random/file.txt b/src/test/fixtures/target/deep/random/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/deep/web/file.txt b/src/test/fixtures/target/deep/web/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/file.txt b/src/test/fixtures/target/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/linux-x64/file.txt b/src/test/fixtures/target/linux-x64/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/package.json b/src/test/fixtures/target/package.json new file mode 100644 index 00000000..1568a134 --- /dev/null +++ b/src/test/fixtures/target/package.json @@ -0,0 +1,6 @@ +{ + "name": "target", + "publisher": "joaomoreno", + "version": "1.0.0", + "engines": { "vscode": "*" } +} \ No newline at end of file diff --git a/src/test/fixtures/target/random/file.txt b/src/test/fixtures/target/random/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/fixtures/target/web/file.txt b/src/test/fixtures/target/web/file.txt new file mode 100644 index 00000000..e69de29b diff --git a/src/test/package.test.ts b/src/test/package.test.ts index c0f15329..2dfddce2 100644 --- a/src/test/package.test.ts +++ b/src/test/package.test.ts @@ -295,6 +295,48 @@ describe('collect', function () { const ignore = files.find(f => f.path === ignoreFilename); assert.ok(!ignore, 'should ignore ' + ignoreFilename) }); + + it('should handle target and ignoreOtherTargetFolders', async function () { + const cwd = fixture('target'); + const manifest = await readManifest(cwd); + let files = await collect(manifest, { cwd }); + + assert.strictEqual(files.length, 13); + assert.ok(files.some(f => f.path === 'extension/file.txt')); + assert.ok(files.some(f => f.path === 'extension/random/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/random/file.txt')); + assert.ok(files.some(f => f.path === 'extension/linux-x64/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/linux-x64/file.txt')); + assert.ok(files.some(f => f.path === 'extension/web/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/web/file.txt')); + assert.ok(files.some(f => f.path === 'extension/darwin-arm64/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/darwin-arm64/file.txt')); + + files = await collect(manifest, { cwd, target: 'linux-x64' }); + + assert.strictEqual(files.length, 13); + assert.ok(files.some(f => f.path === 'extension/file.txt')); + assert.ok(files.some(f => f.path === 'extension/random/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/random/file.txt')); + assert.ok(files.some(f => f.path === 'extension/linux-x64/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/linux-x64/file.txt')); + assert.ok(files.some(f => f.path === 'extension/web/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/web/file.txt')); + assert.ok(files.some(f => f.path === 'extension/darwin-arm64/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/darwin-arm64/file.txt')); + + files = await collect(manifest, { cwd, target: 'linux-x64', ignoreOtherTargetFolders: true }); + + assert.strictEqual(files.length, 9); + assert.ok(files.some(f => f.path === 'extension/file.txt')); + assert.ok(files.some(f => f.path === 'extension/random/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/random/file.txt')); + assert.ok(files.some(f => f.path === 'extension/linux-x64/file.txt')); + assert.ok(files.some(f => f.path === 'extension/deep/linux-x64/file.txt')); + }); }); describe('readManifest', () => {