Skip to content

Commit 4bc98ae

Browse files
committed
Trim package.json in build outputs
1 parent 26fad31 commit 4bc98ae

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

packages/blerf/src/commands/build.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export class BuildEnumerator extends PackageEnumerator {
179179
const packageJson = this.readPackageJson(packageJsonPath);
180180
this.rewriteProjectReferencesFullPath(path.resolve(this.artifactBuildPath), packageJson.dependencies, packages);
181181
this.rewriteProjectReferencesFullPath(path.resolve(this.artifactBuildPath), packageJson.devDependencies, packages);
182+
this.trimPackageJson(packageJson);
182183
fs.writeFileSync(packageJsonPath, stringifyPackage(packageJson), 'utf8');
183184

184185
tar.create({ file: targetTarPath, cwd: tempPath, gzip: true, sync: true, }, ["package"]);

packages/blerf/src/commands/bundle.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ export class BundleEnumerator extends PackageEnumerator {
2323
}
2424

2525
protected async processPackage(packagePath: string, packageJson: any, packages: PackagesType): Promise<void> {
26-
console.log("blerf: bundling node_modules");
27-
28-
// NOTE: assuming file name of tarball; can also get it from the output of npm pack
26+
console.log("blerf: bundling", packageJson.name);
2927
const tempPath = fs.mkdtempSync(path.join(os.tmpdir(), "blerf-"));
3028
const artifactPackTarPath = path.join(this.artifactPackPath, packageJson.name + "-" + packageJson.version + ".tgz");
3129
const artifactTarPath = path.join(this.artifactDeployPath, packageJson.name + "-" + packageJson.version + ".tgz");

packages/blerf/src/commands/pack.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,46 +24,36 @@ export class PackEnumerator extends PackageEnumerator {
2424
}
2525

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

29-
console.log("blerf: patching project references");
30-
31-
// NOTE: assuming file name of tarball; can also get it from the output of npm pack
3230
const sourcePackageTarPath = path.join(packagePath, packageJson.name + "-" + packageJson.version + ".tgz");
3331
const tempPath = fs.mkdtempSync(path.join(os.tmpdir(), "blerf-"));
34-
3532
const artifactPackTarPath = path.join(this.artifactPackPath, packageJson.name + "-" + packageJson.version + ".tgz");
3633

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

3936
try {
4037
tar.extract({ file: sourcePackageTarPath, cwd: tempPath, sync: true });
41-
this.patchPackageJson(packagePath, path.join(tempPath, "package", "package.json"), path.resolve(this.artifactPackPath), packages);
38+
39+
const packageJsonPath = path.join(tempPath, "package", "package.json");
40+
const packageJson = this.readPackageJson(packageJsonPath);
4241
if (this.isDeploy) {
42+
const artifactPackFullPath = path.resolve(this.artifactPackPath);
43+
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.dependencies, packages);
44+
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.devDependencies, packages);
4345
fs.copyFileSync(path.join(packagePath, "package-lock.json"), path.join(tempPath, "package", "package-lock.json"));
46+
} else {
47+
this.rewriteProjectReferencesVersion(packageJson.dependencies, packages);
48+
this.rewriteProjectReferencesVersion(packageJson.devDependencies, packages);
4449
}
50+
51+
this.trimPackageJson(packageJson);
52+
fs.writeFileSync(packageJsonPath, stringifyPackage(packageJson), 'utf8');
4553
tar.create({ file: artifactPackTarPath, cwd: tempPath, gzip: true, sync: true, }, ["package"]);
4654
} finally {
4755
fs.unlinkSync(sourcePackageTarPath);
4856
this.rimraf(tempPath);
4957
}
5058
}
51-
52-
private patchPackageJson(packagePath: string, packageJsonPath: string, artifactPackFullPath: string, packages: PackagesType) {
53-
// Resolve all file:-based dependencies to explicit versions
54-
const packageJson = this.readPackageJson(packageJsonPath);
55-
if (this.isDeploy) {
56-
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.dependencies, packages);
57-
this.rewriteProjectReferencesFullPathVersion(artifactPackFullPath, packageJson.devDependencies, packages);
58-
} else {
59-
this.rewriteProjectReferencesVersion(packageJson.dependencies, packages);
60-
this.rewriteProjectReferencesVersion(packageJson.devDependencies, packages);
61-
}
62-
63-
// Remove stuff not needed in "binary" packge
64-
delete packageJson.scripts;
65-
delete packageJson.blerf;
66-
delete packageJson.devDependencies;
67-
fs.writeFileSync(packageJsonPath, stringifyPackage(packageJson), 'utf8');
68-
}
6959
}

packages/blerf/src/packageEnumerator.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,13 @@ export abstract class PackageEnumerator {
166166
}
167167
}
168168

169+
protected trimPackageJson(packageJson: any) {
170+
// Remove stuff not needed in "binary" packge
171+
// TODO: remove everything except known keys
172+
delete packageJson.scripts;
173+
delete packageJson.blerf;
174+
delete packageJson.devDependencies;
175+
}
176+
169177
protected abstract async processPackage(packagePath: string, packageJson: any, packages: PackagesType): Promise<void>;
170178
}

0 commit comments

Comments
 (0)