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', () => {