planning-space 0.0.1

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.
Files changed (3) hide show
  1. package/README.md +21 -0
  2. package/dist/cli.js +49 -0
  3. package/package.json +27 -0
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # planning-space
2
+
3
+ CLI that creates a new [npm workspace](https://docs.npmjs.com/cli/using-npm/workspaces) inside your current project.
4
+
5
+ ## Usage
6
+
7
+ ```sh
8
+ npx planning-space # creates ./planning-space/ and registers it as a workspace
9
+ npx planning-space my-folder # creates ./my-folder/ instead
10
+ ```
11
+
12
+ It will:
13
+
14
+ - create the target folder with a minimal `package.json`;
15
+ - add the folder to the `workspaces` array of the root `package.json`
16
+ in the current directory (creating that root file if it does not exist);
17
+ - abort with an error if the target folder already exists.
18
+
19
+ ## License
20
+
21
+ MIT
package/dist/cli.js ADDED
@@ -0,0 +1,49 @@
1
+ #!/usr/bin/env node
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
3
+ import { basename, dirname, join, resolve } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ const scriptDir = dirname(fileURLToPath(import.meta.url));
6
+ const ownPkg = JSON.parse(readFileSync(join(scriptDir, "..", "package.json"), "utf8"));
7
+ const requested = process.argv[2]?.trim();
8
+ const workspaceName = requested && requested.length > 0 ? requested : ownPkg.name;
9
+ // A workspace folder name must be filesystem- and package-name-safe.
10
+ if (!/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(workspaceName)) {
11
+ console.error(`Error: invalid workspace name "${workspaceName}".`);
12
+ process.exit(1);
13
+ }
14
+ const cwd = process.cwd();
15
+ const targetDir = resolve(cwd, workspaceName);
16
+ if (targetDir === cwd || existsSync(targetDir)) {
17
+ console.error(`Error: folder "${workspaceName}" already exists. Aborting.`);
18
+ process.exit(1);
19
+ }
20
+ const rootPkgPath = join(cwd, "package.json");
21
+ const rootPkg = existsSync(rootPkgPath)
22
+ ? JSON.parse(readFileSync(rootPkgPath, "utf8"))
23
+ : { name: basename(cwd), version: "0.0.0", private: true };
24
+ // npm accepts `workspaces` as an array; some tools use `{ packages: [] }`.
25
+ let workspaces;
26
+ const ws = rootPkg.workspaces;
27
+ if (Array.isArray(ws)) {
28
+ workspaces = ws;
29
+ }
30
+ else if (ws && typeof ws === "object" && Array.isArray(ws.packages)) {
31
+ workspaces = ws.packages;
32
+ }
33
+ else {
34
+ workspaces = [];
35
+ rootPkg.workspaces = workspaces;
36
+ }
37
+ if (!workspaces.includes(workspaceName)) {
38
+ workspaces.push(workspaceName);
39
+ }
40
+ mkdirSync(targetDir);
41
+ const childPkg = {
42
+ name: workspaceName,
43
+ version: "0.0.0",
44
+ private: true,
45
+ };
46
+ writeFileSync(join(targetDir, "package.json"), `${JSON.stringify(childPkg, null, 2)}\n`);
47
+ writeFileSync(rootPkgPath, `${JSON.stringify(rootPkg, null, 2)}\n`);
48
+ console.log(`Created npm workspace "${workspaceName}" at ${targetDir}`);
49
+ console.log(`Registered it in ${rootPkgPath}`);
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "planning-space",
3
+ "version": "0.0.1",
4
+ "description": "CLI that scaffolds a new npm workspace (default folder name \"planning-space\") inside the current project.",
5
+ "type": "module",
6
+ "bin": {
7
+ "planning-space": "dist/cli.js"
8
+ },
9
+ "files": [
10
+ "dist"
11
+ ],
12
+ "keywords": [
13
+ "workspace",
14
+ "workspaces",
15
+ "monorepo",
16
+ "scaffold",
17
+ "cli"
18
+ ],
19
+ "scripts": {
20
+ "build": "tsc",
21
+ "prepublishOnly": "npm run build"
22
+ },
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "license": "MIT"
27
+ }