Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new: Add validate files option to verify distributable files. #33

Merged
merged 6 commits into from
Mar 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ esm/
lib/
mjs/
tmp/
umd/
node_modules/

# Custom
Expand Down
1 change: 0 additions & 1 deletion .npmignore

This file was deleted.

16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
"files": [
"api-extractor.json",
"babel.js",
"dts/",
"lib/",
"src/"
"dts/**/*.d.ts",
"lib/**/*.{js,map}",
"src/**/*.{ts,tsx,json}"
],
"exports": {
"./package.json": "./package.json",
Expand Down Expand Up @@ -84,13 +84,14 @@
"devDependencies": {
"@boost/test-utils": "^2.3.0",
"@milesj/build-tools": "^2.17.1",
"@types/fs-extra": "^9.0.7",
"@types/fs-extra": "^9.0.8",
"@types/node": "^14.14.31",
"@types/npm-packlist": "^1.1.1",
"@types/react": "^16.9.56",
"@types/rimraf": "^3.0.0",
"@types/semver": "^7.3.4",
"chokidar": "^3.5.1",
"typescript": "^4.2.2"
"typescript": "^4.2.3"
},
"dependencies": {
"@babel/core": "^7.12.16",
Expand All @@ -106,7 +107,7 @@
"@boost/event": "^2.3.1",
"@boost/pipeline": "^2.2.4",
"@boost/terminal": "^2.2.0",
"@microsoft/api-extractor": "^7.13.1",
"@microsoft/api-extractor": "^7.13.2",
"@rollup/plugin-babel": "^5.3.0",
"@rollup/plugin-commonjs": "^17.1.0",
"@rollup/plugin-node-resolve": "^11.2.0",
Expand All @@ -119,6 +120,7 @@
"ink": "^3.0.8",
"ink-progress-bar": "^3.0.0",
"ink-spinner": "^4.0.1",
"npm-packlist": "^2.1.4",
"react": "^16.14.0",
"rimraf": "^3.0.2",
"rollup": "^2.40.0",
Expand All @@ -130,7 +132,7 @@
},
"optionalDependencies": {
"chokidar": "^3.5.1",
"typescript": "^4.2.2"
"typescript": "^4.2.3"
},
"funding": {
"type": "ko-fi",
Expand Down
34 changes: 30 additions & 4 deletions src/Package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,9 @@ export class Package {
const files = new Set<string>(this.packageJson.files);

this.artifacts.forEach((artifact) => {
// Build files
if (artifact instanceof BundleArtifact) {
// Generate `main` and `module` fields
if (artifact.outputName === 'index') {
if (!mainEntry || artifact.platform === 'node') {
mainEntry = artifact.findEntryPoint(['lib', 'cjs', 'mjs']);
Expand All @@ -298,6 +300,7 @@ export class Package {
}
}

// Generate `bin` field
if (
artifact.outputName === 'bin' &&
artifact.platform === 'node' &&
Expand All @@ -306,12 +309,17 @@ export class Package {
this.packageJson.bin = artifact.findEntryPoint(['lib', 'cjs', 'mjs']);
}

artifact.builds.forEach((build) => {
files.add(`${build.format}/`);
// Generate `files` list
artifact.builds.forEach(({ format }) => {
files.add(`${format}/**/*.{${artifact.getOutputMetadata(format).ext},map}`);
});

files.add(`src/**/*.{${this.getSourceFileExts(artifact.inputFile)}}`);

// Type declarations
} else if (artifact instanceof TypesArtifact) {
this.packageJson.types = './dts/index.d.ts';
files.add('dts/');
files.add('dts/**/*.d.ts');
}
});

Expand All @@ -329,7 +337,6 @@ export class Package {
this.packageJson.module = moduleEntry;
}

files.add('src/');
this.packageJson.files = Array.from(files).sort();
}

Expand Down Expand Up @@ -364,4 +371,23 @@ export class Package {

this.packageJson.exports = exports as PackageStructure['exports'];
}

protected getSourceFileExts(inputFile: string): string[] {
const sourceExt = new Path(inputFile).ext(true);
const exts: string[] = [sourceExt];

if (sourceExt === 'js') {
exts.push('jsx');
} else if (sourceExt === 'jsx' || sourceExt === 'cjs') {
exts.push('js');
} else if (sourceExt === 'ts') {
exts.push('tsx');
} else if (sourceExt === 'tsx') {
exts.push('ts');
}

exts.push('json');

return exts;
}
}
63 changes: 63 additions & 0 deletions src/PackageValidator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import http from 'http';
import https from 'https';
import execa from 'execa';
import glob from 'fast-glob';
import packList from 'npm-packlist';
import semver from 'semver';
import spdxLicenses from 'spdx-license-list';
import { DependencyMap, isModuleName, isObject, PeopleSetting, toArray } from '@boost/common';
Expand Down Expand Up @@ -47,6 +49,10 @@ export class PackageValidator {
this.checkEntryPoints();
}

if (options.files) {
promises.push(this.checkFiles());
}

if (options.license) {
this.checkLicense();
}
Expand Down Expand Up @@ -214,6 +220,41 @@ export class PackageValidator {
}
}

protected async checkFiles() {
const futureFiles = new Set(await packList({ path: this.package.path.path() }));
const presentFiles = new Set(await this.findDistributableFiles());

// First check that our files are in the potential NPM list
const ignored = new Set<string>();

presentFiles.forEach((file) => {
if (!futureFiles.has(file)) {
ignored.add(file);
}
});

if (ignored.size > 0) {
this.errors.push(
`The following files are being ignored from publishing: ${Array.from(ignored).join(', ')}`,
);
}

// Then check that NPM isnt adding something unwanted
const unwanted = new Set<string>();

futureFiles.forEach((file) => {
if (!presentFiles.has(file)) {
unwanted.add(file);
}
});

if (unwanted.size > 0) {
this.warnings.push(
`The following files are being inadvertently published: ${Array.from(unwanted).join(', ')}`,
);
}
}

protected checkLicense() {
this.package.debug('Checking license');

Expand Down Expand Up @@ -381,6 +422,28 @@ export class PackageValidator {
});
}

protected async findDistributableFiles(): Promise<string[]> {
// https://github.com/npm/npm-packlist/blob/master/index.js#L29
const patterns: string[] = [
'(readme|copying|license|licence|notice|changes|changelog|history)*',
'package.json',
];

this.package.packageJson.files?.forEach((file) => {
if (file.endsWith('/')) {
patterns.push(`${file}**/*.{json,js,jsx,cjs,mjs,ts,tsx,map}`);
} else {
patterns.push(file);
}
});

return glob(patterns, {
caseSensitiveMatch: false,
cwd: this.package.path.path(),
ignore: ['node_modules'],
});
}

protected async getBinVersion(bin: string): Promise<string> {
try {
return (await execa(bin, ['-v'], { preferLocal: true })).stdout.trim();
Expand Down
1 change: 1 addition & 0 deletions src/babel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export function createConfig(folder: string): ConfigStructure {
const platforms = pkg.configs.map((config) => config.platform);
let lowestPlatform: Platform = 'node';

// istanbul ignore next
if (platforms.includes('browser')) {
lowestPlatform = 'browser';
} else if (platforms.includes('native')) {
Expand Down
4 changes: 4 additions & 0 deletions src/commands/Validate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export class ValidateCommand extends BaseCommand<Required<ValidateOptions>> {
@Arg.Flag('Check that `main`, `module`, and other entry points are valid paths')
entries: boolean = true;

@Arg.Flag('Check that distributable files are not being accidentally ignored')
files: boolean = true;

@Arg.Flag('Check that a SPDX license is provided')
license: boolean = true;

Expand All @@ -37,6 +40,7 @@ export class ValidateCommand extends BaseCommand<Required<ValidateOptions>> {
deps={this.deps}
engines={this.engines}
entries={this.engines}
files={this.files}
license={this.license}
links={this.links}
meta={this.meta}
Expand Down
1 change: 1 addition & 0 deletions src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export const validateBlueprint: Blueprint<ValidateOptions> = {
deps: bool(true),
engines: bool(true),
entries: bool(true),
files: bool(true),
license: bool(true),
links: bool(true),
meta: bool(true),
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export interface ValidateOptions {
deps?: boolean;
engines?: boolean;
entries?: boolean;
files?: boolean;
license?: boolean;
links?: boolean;
meta?: boolean;
Expand Down
Loading