Skip to content

Commit

Permalink
Trim package.json in build outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
andersnm committed Apr 23, 2019
1 parent 26fad31 commit 4bc98ae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
1 change: 1 addition & 0 deletions packages/blerf/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ export class BuildEnumerator extends PackageEnumerator {
const packageJson = this.readPackageJson(packageJsonPath);
this.rewriteProjectReferencesFullPath(path.resolve(this.artifactBuildPath), packageJson.dependencies, packages);
this.rewriteProjectReferencesFullPath(path.resolve(this.artifactBuildPath), packageJson.devDependencies, packages);
this.trimPackageJson(packageJson);
fs.writeFileSync(packageJsonPath, stringifyPackage(packageJson), 'utf8');

tar.create({ file: targetTarPath, cwd: tempPath, gzip: true, sync: true, }, ["package"]);
Expand Down
4 changes: 1 addition & 3 deletions packages/blerf/src/commands/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export class BundleEnumerator extends PackageEnumerator {
}

protected async processPackage(packagePath: string, packageJson: any, packages: PackagesType): Promise<void> {
console.log("blerf: bundling node_modules");

// NOTE: assuming file name of tarball; can also get it from the output of npm pack
console.log("blerf: bundling", packageJson.name);
const tempPath = fs.mkdtempSync(path.join(os.tmpdir(), "blerf-"));
const artifactPackTarPath = path.join(this.artifactPackPath, packageJson.name + "-" + packageJson.version + ".tgz");
const artifactTarPath = path.join(this.artifactDeployPath, packageJson.name + "-" + packageJson.version + ".tgz");
Expand Down
36 changes: 13 additions & 23 deletions packages/blerf/src/commands/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,36 @@ export class PackEnumerator extends PackageEnumerator {
}

protected async processPackage(packagePath: string, packageJson: any, packages: PackagesType): Promise<void> {
console.log("blerf: packing and patching", packageJson.name);
childProcess.execSync("npm pack", {stdio: 'inherit', cwd: packagePath});

console.log("blerf: patching project references");

// NOTE: assuming file name of tarball; can also get it from the output of npm pack
const sourcePackageTarPath = path.join(packagePath, packageJson.name + "-" + packageJson.version + ".tgz");
const tempPath = fs.mkdtempSync(path.join(os.tmpdir(), "blerf-"));

const artifactPackTarPath = path.join(this.artifactPackPath, packageJson.name + "-" + packageJson.version + ".tgz");

fs.mkdirSync(this.artifactPackPath, { recursive: true });

try {
tar.extract({ file: sourcePackageTarPath, cwd: tempPath, sync: true });
this.patchPackageJson(packagePath, path.join(tempPath, "package", "package.json"), path.resolve(this.artifactPackPath), packages);

const packageJsonPath = path.join(tempPath, "package", "package.json");
const packageJson = this.readPackageJson(packageJsonPath);
if (this.isDeploy) {
const artifactPackFullPath = path.resolve(this.artifactPackPath);
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.dependencies, packages);
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.devDependencies, packages);
fs.copyFileSync(path.join(packagePath, "package-lock.json"), path.join(tempPath, "package", "package-lock.json"));
} else {
this.rewriteProjectReferencesVersion(packageJson.dependencies, packages);
this.rewriteProjectReferencesVersion(packageJson.devDependencies, packages);
}

this.trimPackageJson(packageJson);
fs.writeFileSync(packageJsonPath, stringifyPackage(packageJson), 'utf8');
tar.create({ file: artifactPackTarPath, cwd: tempPath, gzip: true, sync: true, }, ["package"]);
} finally {
fs.unlinkSync(sourcePackageTarPath);
this.rimraf(tempPath);
}
}

private patchPackageJson(packagePath: string, packageJsonPath: string, artifactPackFullPath: string, packages: PackagesType) {
// Resolve all file:-based dependencies to explicit versions
const packageJson = this.readPackageJson(packageJsonPath);
if (this.isDeploy) {
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.dependencies, packages);
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.devDependencies, packages);
} else {
this.rewriteProjectReferencesVersion(packageJson.dependencies, packages);
this.rewriteProjectReferencesVersion(packageJson.devDependencies, packages);
}

// Remove stuff not needed in "binary" packge
delete packageJson.scripts;
delete packageJson.blerf;
delete packageJson.devDependencies;
fs.writeFileSync(packageJsonPath, stringifyPackage(packageJson), 'utf8');
}
}
8 changes: 8 additions & 0 deletions packages/blerf/src/packageEnumerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,13 @@ export abstract class PackageEnumerator {
}
}

protected trimPackageJson(packageJson: any) {
// Remove stuff not needed in "binary" packge
// TODO: remove everything except known keys
delete packageJson.scripts;
delete packageJson.blerf;
delete packageJson.devDependencies;
}

protected abstract async processPackage(packagePath: string, packageJson: any, packages: PackagesType): Promise<void>;
}

0 comments on commit 4bc98ae

Please sign in to comment.