@alcalzone/monopack 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 AlCalzone
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,14 @@
1
+ # monopack
2
+
3
+ Like `npm pack` or `yarn pack`, but for entire monorepos.
4
+ This works by editing the `package.json` files in the resulting tarballs so that they reference each other.
5
+
6
+ ## Usage
7
+
8
+ Run this inside the root of your monorepo:
9
+
10
+ ```sh
11
+ npx @alcalzone/monopack [--target <target-dir>]
12
+ ```
13
+
14
+ To specify a target directory, use the `--target` flag. By default, the tarballs will be created in the `.prodpack` directory.
package/bin/cli.js ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ require("../build/cli.js");
package/build/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ export {};
package/build/cli.js ADDED
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ // Script to pack the monorepo packages for production, but locally
7
+ const fs_extra_1 = __importDefault(require("fs-extra"));
8
+ const path_1 = __importDefault(require("path"));
9
+ const tar_stream_1 = __importDefault(require("tar-stream"));
10
+ const zlib_1 = __importDefault(require("zlib"));
11
+ const pak_1 = require("@alcalzone/pak");
12
+ async function stream2buffer(stream) {
13
+ return new Promise((resolve, reject) => {
14
+ const _buf = Array();
15
+ stream.on("data", (chunk) => _buf.push(chunk));
16
+ stream.on("end", () => resolve(Buffer.concat(_buf)));
17
+ stream.on("error", (err) => reject(`error converting stream - ${err}`));
18
+ });
19
+ }
20
+ async function main() {
21
+ const workspaces = [];
22
+ const targetArgsIndex = process.argv.indexOf("--target");
23
+ let outDir = (targetArgsIndex > -1 && process.argv[targetArgsIndex + 1]) ||
24
+ ".prodpack";
25
+ if (!path_1.default.isAbsolute(outDir)) {
26
+ outDir = path_1.default.join(process.cwd(), outDir);
27
+ }
28
+ // First pass: read all package.json files
29
+ console.log("Parsing workspace...");
30
+ const pak = await (0, pak_1.detectPackageManager)();
31
+ const workspaceDirs = await pak.workspaces();
32
+ for (const workspaceDir of workspaceDirs) {
33
+ const packageJson = await fs_extra_1.default.readJson(path_1.default.join(workspaceDir, "package.json"));
34
+ if (packageJson.private)
35
+ continue;
36
+ const { name, version } = packageJson;
37
+ workspaces.push({
38
+ name,
39
+ dir: workspaceDir,
40
+ version,
41
+ packageJson,
42
+ workspaceDependencies: [],
43
+ tarball: undefined, // will be set later
44
+ });
45
+ }
46
+ // Second pass: find all workspace dependencies
47
+ for (const workspace of workspaces) {
48
+ const { dependencies } = workspace.packageJson;
49
+ if (!dependencies)
50
+ continue;
51
+ for (const dependency of Object.keys(dependencies)) {
52
+ const isOwn = workspaces.some((w) => w.name === dependency);
53
+ if (isOwn) {
54
+ workspace.workspaceDependencies.push(dependency);
55
+ }
56
+ }
57
+ }
58
+ // Pack all workspaces
59
+ console.log("Packing tarballs...");
60
+ await fs_extra_1.default.ensureDir(outDir);
61
+ for (const workspace of workspaces) {
62
+ console.log(` ${workspace.name}`);
63
+ const result = await pak.pack({
64
+ workspace: path_1.default.relative(pak.cwd, workspace.dir),
65
+ targetDir: outDir,
66
+ });
67
+ if (result.success) {
68
+ workspace.tarball = result.stdout;
69
+ }
70
+ else {
71
+ console.error(result.stderr);
72
+ process.exit(1);
73
+ }
74
+ }
75
+ // Modify each tarball to point at the other tarballs
76
+ console.log("Modifying workspaces...");
77
+ for (const workspace of workspaces) {
78
+ console.log(` ${workspace.name}`);
79
+ const extract = tar_stream_1.default.extract();
80
+ const pack = tar_stream_1.default.pack();
81
+ extract.on("entry", async (header, stream, next) => {
82
+ var _a;
83
+ if (header.name === "package/package.json") {
84
+ const data = await stream2buffer(stream);
85
+ const packageJson = JSON.parse(data.toString());
86
+ // Replace workspace dependencies with references to local tarballs
87
+ for (const dep of workspace.workspaceDependencies) {
88
+ const tarball = (_a = workspaces.find((w) => w.name === dep)) === null || _a === void 0 ? void 0 : _a.tarball;
89
+ if (!tarball) {
90
+ console.error(`Found no tarball for ${dep}, required by ${workspace.name}`);
91
+ process.exit(1);
92
+ }
93
+ packageJson.dependencies[dep] = `file:./${path_1.default.basename(tarball)}`;
94
+ }
95
+ // Avoid accidentally installing dev dependencies
96
+ delete packageJson.devDependencies;
97
+ // Return data
98
+ pack.entry(header, JSON.stringify(packageJson, null, 2), next);
99
+ }
100
+ else {
101
+ // pass through
102
+ stream.pipe(pack.entry(header, next));
103
+ }
104
+ });
105
+ extract.on("finish", () => {
106
+ pack.finalize();
107
+ });
108
+ const read = fs_extra_1.default.createReadStream(workspace.tarball);
109
+ const unzip = zlib_1.default.createGunzip();
110
+ read.pipe(unzip).pipe(extract);
111
+ const zip = zlib_1.default.createGzip();
112
+ const write = fs_extra_1.default.createWriteStream(workspace.tarball + ".tmp");
113
+ pack.pipe(zip).pipe(write);
114
+ await new Promise((resolve) => write.on("finish", resolve));
115
+ // Replace the original tarball
116
+ await fs_extra_1.default.unlink(workspace.tarball);
117
+ await fs_extra_1.default.rename(workspace.tarball + ".tmp", workspace.tarball);
118
+ }
119
+ console.log("Done!");
120
+ }
121
+ main().catch((err) => {
122
+ console.error(err);
123
+ process.exit(1);
124
+ });
125
+ //# sourceMappingURL=cli.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";;;;;AAAA,mEAAmE;AACnE,wDAA0B;AAC1B,gDAAwB;AAExB,4DAA6B;AAC7B,gDAAwB;AACxB,wCAAsD;AAWtD,KAAK,UAAU,aAAa,CAAC,MAAc;IAC1C,OAAO,IAAI,OAAO,CAAS,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QAC9C,MAAM,IAAI,GAAG,KAAK,EAAO,CAAC;QAE1B,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC;QAC/C,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;QACrD,MAAM,CAAC,EAAE,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,6BAA6B,GAAG,EAAE,CAAC,CAAC,CAAC;IACzE,CAAC,CAAC,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,IAAI;IAClB,MAAM,UAAU,GAAgB,EAAE,CAAC;IAEnC,MAAM,eAAe,GAAG,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;IAEzD,IAAI,MAAM,GACT,CAAC,eAA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AC,IAAI,CAAC,OAAO,CAAC,CAAC;QAE/B,MAAM,GAAG,GAAG,cAAI,CAAC,UAAU,EAAE,CAAC;QAC9B,MAAM,KAAK,GAAG,kBAAE,CAAC,iBAAiB,CAAC,SAAS,CAAC,OAAO,GAAG,MAAM,CAAC,CAAC;QAC/D,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAE3B,MAAM,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,QAAQ,EAAE,OAAO,CAAC,CAAC,CAAC;QAE5D,+BAA+B;QAC/B,MAAM,kBAAE,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;QACnC,MAAM,kBAAE,CAAC,MAAM,CAAC,SAAS,CAAC,OAAO,GAAG,MAAM,EAAE,SAAS,CAAC,OAAO,CAAC,CAAC;KAC/D;IAED,OAAO,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;AACtB,CAAC;AAED,IAAI,EAAE,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;IACpB,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACnB,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;AACjB,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "name": "@alcalzone/monopack",
3
+ "version": "1.0.0",
4
+ "description": "Like `npm pack` or `yarn pack`, but for entire monorepos.",
5
+ "main": "build/cli.js",
6
+ "types": "build/cli.d.ts",
7
+ "files": [
8
+ "bin/**",
9
+ "build/**"
10
+ ],
11
+ "bin": "bin/cli.js",
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "repository": "https://github.com/AlCalzone/monopack.git",
16
+ "author": "Dominic Griesel <d.griesel@gmx.net>",
17
+ "license": "MIT",
18
+ "scripts": {
19
+ "lint:ts": "eslint --ext .ts \"src/**/*.ts\"",
20
+ "lint": "yarn run lint:ts",
21
+ "prebuild": "rimraf ./build",
22
+ "build": "tsc -p tsconfig.build.json",
23
+ "check": "tsc -p tsconfig.build.json --noEmit",
24
+ "watch": "yarn run build --watch",
25
+ "release": "release-script"
26
+ },
27
+ "devDependencies": {
28
+ "@alcalzone/release-script": "^3.5.9",
29
+ "@types/fs-extra": "^9.0.13",
30
+ "@types/node": "^14.18.29",
31
+ "@types/tar-stream": "^2.2.2",
32
+ "@typescript-eslint/eslint-plugin": "^5.38.0",
33
+ "@typescript-eslint/parser": "^5.38.0",
34
+ "eslint": "^8.23.1",
35
+ "eslint-config-prettier": "^8.5.0",
36
+ "eslint-plugin-prettier": "^4.2.1",
37
+ "prettier": "^2.7.1",
38
+ "prettier-plugin-organize-imports": "^3.1.1",
39
+ "rimraf": "^3.0.2",
40
+ "source-map-support": "^0.5.21",
41
+ "typescript": "~4.8.3"
42
+ },
43
+ "dependencies": {
44
+ "@alcalzone/pak": "^0.9.0",
45
+ "fs-extra": "^10.1.0",
46
+ "tar-stream": "^2.2.0"
47
+ },
48
+ "packageManager": "yarn@3.2.3"
49
+ }