Skip to content

Commit

Permalink
revert ignore module, add tests
Browse files Browse the repository at this point in the history
fixes #588
  • Loading branch information
joaomoreno committed Jun 25, 2021
1 parent edd70e3 commit 599cea7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 22 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
"commander": "^6.1.0",
"denodeify": "^1.2.1",
"glob": "^7.0.6",
"ignore": "^5.1.8",
"leven": "^3.1.0",
"lodash": "^4.17.15",
"markdown-it": "^10.0.0",
"mime": "^1.3.4",
"minimatch": "^3.0.3",
"osenv": "^0.1.3",
"parse-semver": "^1.1.1",
"read": "^1.0.7",
Expand All @@ -64,6 +64,7 @@
"@types/lodash": "^4.14.123",
"@types/markdown-it": "0.0.2",
"@types/mime": "^1",
"@types/minimatch": "^3.0.3",
"@types/mocha": "^7.0.2",
"@types/node": "^10.17.60",
"@types/read": "^0.0.28",
Expand Down
63 changes: 45 additions & 18 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ExtensionKind, Manifest } from './manifest';
import { ITranslations, patchNLS } from './nls';
import * as util from './util';
import * as _glob from 'glob';
import * as minimatch from 'minimatch';
import * as denodeify from 'denodeify';
import * as markdownit from 'markdown-it';
import * as cheerio from 'cheerio';
Expand All @@ -22,7 +23,6 @@ import {
validateVSCodeTypesCompatibility,
} from './validation';
import { detectYarn, getDependencies } from './npm';
import ignore from 'ignore';

const readFile = denodeify<string, string, string>(fs.readFile);
const unlink = denodeify<string, void>(fs.unlink as any);
Expand All @@ -37,6 +37,8 @@ const resourcesPath = path.join(path.dirname(__dirname), 'resources');
const vsixManifestTemplatePath = path.join(resourcesPath, 'extension.vsixmanifest');
const contentTypesTemplatePath = path.join(resourcesPath, '[Content_Types].xml');

const MinimatchOptions: minimatch.IOptions = { dot: true };

export interface IInMemoryFile {
path: string;
mode?: number;
Expand Down Expand Up @@ -1118,28 +1120,53 @@ function collectAllFiles(cwd: string, useYarn?: boolean, dependencyEntryPoints?:
});
}

async function readIgnoreFile(cwd: string, ignoreFile?: string): Promise<string> {
try {
return await readFile(ignoreFile ? ignoreFile : path.join(cwd, '.vscodeignore'), 'utf8');
} catch (err) {
if (err.code !== 'ENOENT' || ignoreFile) {
throw err;
}

return '';
}
}

async function collectFiles(
function collectFiles(
cwd: string,
useYarn?: boolean,
dependencyEntryPoints?: string[],
ignoreFile?: string
): Promise<string[]> {
const files = (await collectAllFiles(cwd, useYarn, dependencyEntryPoints)).filter(f => !/\r$/m.test(f));
const rawIgnore = await readIgnoreFile(cwd, ignoreFile);

return ignore().add(defaultIgnore).add(rawIgnore).add(notIgnored).filter(files);
return collectAllFiles(cwd, useYarn, dependencyEntryPoints).then(files => {
files = files.filter(f => !/\r$/m.test(f));

return (
readFile(ignoreFile ? ignoreFile : path.join(cwd, '.vscodeignore'), 'utf8')
.catch<string>(err =>
err.code !== 'ENOENT' ? Promise.reject(err) : ignoreFile ? Promise.reject(err) : Promise.resolve('')
)

// Parse raw ignore by splitting output into lines and filtering out empty lines and comments
.then(rawIgnore =>
rawIgnore
.split(/[\n\r]/)
.map(s => s.trim())
.filter(s => !!s)
.filter(i => !/^\s*#/.test(i))
)

// Add '/**' to possible folder names
.then(ignore => [
...ignore,
...ignore.filter(i => !/(^|\/)[^/]*\*[^/]*$/.test(i)).map(i => (/\/$/.test(i) ? `${i}**` : `${i}/**`)),
])

// Combine with default ignore list
.then(ignore => [...defaultIgnore, ...ignore, ...notIgnored])

// Split into ignore and negate list
.then(ignore => _.partition(ignore, i => !/^\s*!/.test(i)))
.then(r => ({ ignore: r[0], negate: r[1] }))

// Filter out files
.then(({ ignore, negate }) =>
files.filter(
f =>
!ignore.some(i => minimatch(f, i, MinimatchOptions)) ||
negate.some(i => minimatch(f, i.substr(1), MinimatchOptions))
)
)
);
});
}

export function processFiles(processors: IProcessor[], files: IFile[]): Promise<IFile[]> {
Expand Down
7 changes: 6 additions & 1 deletion src/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,12 @@ describe('collect', function () {
const files = await collect(manifest, { cwd });
const names = files.map(f => f.path).sort();

assert.deepStrictEqual(names, ['[Content_Types].xml', 'extension.vsixmanifest', 'extension/package.json']);
assert.deepStrictEqual(names, [
'[Content_Types].xml',
'extension.vsixmanifest',
'extension/foo/bar/hello.txt',
'extension/package.json',
]);
});

it('should ignore devDependencies', () => {
Expand Down

0 comments on commit 599cea7

Please sign in to comment.