@w5s/managed-script-config 1.0.0-alpha.2 → 1.0.0-alpha.5

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 (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +2 -0
  3. package/dist/index.d.ts +3 -27
  4. package/dist/index.js +26 -7
  5. package/dist/index.js.map +1 -1
  6. package/dist/isGit-B965siHE.js +6 -0
  7. package/dist/isGit-B965siHE.js.map +1 -0
  8. package/dist/npmCommandOrThrow-D7r_D9iV.js +28 -0
  9. package/dist/npmCommandOrThrow-D7r_D9iV.js.map +1 -0
  10. package/dist/npmExec-B2YFcpLE.js +27 -0
  11. package/dist/npmExec-B2YFcpLE.js.map +1 -0
  12. package/dist/script/prepare/gitmodules.d.ts +1 -0
  13. package/dist/script/prepare/gitmodules.js +13 -0
  14. package/dist/script/prepare/gitmodules.js.map +1 -0
  15. package/dist/script/prepare/lefthook.d.ts +1 -0
  16. package/dist/script/prepare/lefthook.js +11 -0
  17. package/dist/script/prepare/lefthook.js.map +1 -0
  18. package/dist/script/prepare/skills.d.ts +1 -0
  19. package/dist/script/prepare/skills.js +67 -0
  20. package/dist/script/prepare/skills.js.map +1 -0
  21. package/dist/script/prepare.d.ts +1 -0
  22. package/dist/script/prepare.js +50 -0
  23. package/dist/script/prepare.js.map +1 -0
  24. package/dist/script/rescue.d.ts +1 -1
  25. package/dist/script/rescue.js +32 -6
  26. package/dist/script/rescue.js.map +1 -1
  27. package/package.json +9 -3
  28. package/src/config.ts +22 -3
  29. package/src/internal/detectAgent.ts +25 -0
  30. package/src/internal/isGit.ts +3 -0
  31. package/src/internal/npmCommand.ts +1 -0
  32. package/src/internal/npmCommandOrThrow.ts +11 -0
  33. package/src/internal/npmExec.ts +28 -0
  34. package/src/internal/npmInstall.ts +28 -0
  35. package/src/internal/npmRun.ts +28 -0
  36. package/src/internal/npmRunAll.ts +46 -0
  37. package/src/internal/pathExists.ts +21 -0
  38. package/src/internal/skillsAgentList.ts +39 -0
  39. package/src/script/prepare/gitmodules.ts +15 -0
  40. package/src/script/prepare/lefthook.ts +12 -0
  41. package/src/script/prepare/skills.ts +18 -0
  42. package/src/script/prepare.ts +9 -0
  43. package/src/script/rescue.ts +11 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Julien Polo
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 CHANGED
@@ -29,6 +29,8 @@ In your `managed-script.config.json` (or the `"managed-script"` field of `packag
29
29
 
30
30
  This provides the following scripts:
31
31
 
32
+ - `prepare:lefthook`: installs the lefthook git hooks (skipped in CI or outside a git repository).
33
+ - `prepare:gitmodules`: runs `git submodule sync --recursive` then `git submodule update --init --recursive`, only when a `.gitmodules` file exists at the repository root.
32
34
  - `rescue`: `git clean -fdx` then `pnpm install`. The implementation lives in [`rescue.ts`](./src/rescue.ts), invoked through the `MANAGED_SCRIPT_CONFIG_DIR` environment variable so it resolves regardless of the consuming project's working directory.
33
35
 
34
36
  ## License
package/dist/index.d.ts CHANGED
@@ -1,32 +1,8 @@
1
- //#region ../../node_modules/.pnpm/c12@3.3.4/node_modules/c12/dist/index.d.mts
2
- declare global {
3
- var __c12_dotenv_vars__: Map<Record<string, any>, Set<string>>;
4
- }
5
- //#endregion
6
- //#region src/types.d.ts
7
- interface ConfigLayerMeta {
8
- name?: string;
9
- [key: string]: any;
10
- }
11
- type UserInputConfig = Record<string, any>;
12
- interface C12InputConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> {
13
- $test?: T;
14
- $development?: T;
15
- $production?: T;
16
- $env?: Record<string, T>;
17
- $meta?: MT;
18
- }
19
- type InputConfig<T extends UserInputConfig = UserInputConfig, MT extends ConfigLayerMeta = ConfigLayerMeta> = C12InputConfig<T, MT> & T;
20
- //#endregion
21
- //#region src/config.d.ts
22
- declare const config: InputConfig<import("@w5s/managed-script").UserConfig, ConfigLayerMeta>;
23
- //#endregion
24
- //#region src/meta.d.ts
1
+ import { UserConfig } from "@w5s/managed-script";
2
+ declare const config: UserConfig;
25
3
  export declare const meta: Readonly<{
26
4
  buildNumber: number;
27
5
  name: string;
28
6
  version: string;
29
7
  }>;
30
- //#endregion
31
- export { config as default };
32
- //# sourceMappingURL=index.d.ts.map
8
+ export { config as default };
package/dist/index.js CHANGED
@@ -1,16 +1,35 @@
1
1
  import { defineConfig } from "@w5s/managed-script";
2
2
  import path from "node:path";
3
- //#region src/config.ts
3
+ // src/config.ts
4
4
  const configDir = new URL(".", import.meta.url).pathname;
5
- const config = defineConfig({ scripts: { rescue: `node "${path.join(configDir, "script", "rescue.js")}"` } });
6
- //#endregion
7
- //#region src/meta.ts
5
+ const config = defineConfig({ scripts: {
6
+ "prepare": {
7
+ args: [path.join(configDir, "script", "prepare.js")],
8
+ runner: "node"
9
+ },
10
+ "prepare:gitmodules": {
11
+ args: [path.join(configDir, "script", "prepare", "gitmodules.js")],
12
+ runner: "node"
13
+ },
14
+ "prepare:lefthook": {
15
+ args: [path.join(configDir, "script", "prepare", "lefthook.js")],
16
+ runner: "node"
17
+ },
18
+ "prepare:skills": {
19
+ args: [path.join(configDir, "script", "prepare", "skills.js")],
20
+ runner: "node"
21
+ },
22
+ "rescue": {
23
+ args: [path.join(configDir, "script", "rescue.js")],
24
+ runner: "node"
25
+ }
26
+ } });
27
+ // src/meta.ts
8
28
  const meta = Object.freeze({
9
- buildNumber: 0,
29
+ buildNumber: 0x6b04cca5,
10
30
  name: "@w5s/managed-script-config",
11
- version: "1.0.0-alpha.2"
31
+ version: "1.0.0-alpha.5"
12
32
  });
13
- //#endregion
14
33
  export { config as default, meta };
15
34
 
16
35
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/config.ts","../src/meta.ts"],"sourcesContent":["import { defineConfig } from '@w5s/managed-script';\nimport path from 'node:path';\n\nconst configDir = new URL('.', import.meta.url).pathname;\n\nexport const config = defineConfig({\n scripts: {\n rescue: `node \"${path.join(configDir, 'script', 'rescue.js')}\"`,\n },\n});\n","export const meta = Object.freeze({\n // @ts-ignore - these variables are injected at build time\n buildNumber: (typeof __PACKAGE_BUILD_NUMBER__ === 'undefined' ? 0 : __PACKAGE_BUILD_NUMBER__) as number,\n // @ts-ignore - these variables are injected at build time\n name: (typeof __PACKAGE_NAME__ === 'undefined' ? '' : __PACKAGE_NAME__) as string,\n // @ts-ignore - these variables are injected at build time\n version: (typeof __PACKAGE_VERSION__ === 'undefined' ? '' : __PACKAGE_VERSION__) as string,\n});\n"],"mappings":";;;AAGA,MAAM,YAAY,IAAI,IAAI,KAAK,YAAY,GAAG,CAAC,CAAC;AAEhD,MAAa,SAAS,aAAa,EACjC,SAAS,EACP,QAAQ,SAAS,KAAK,KAAK,WAAW,UAAU,WAAW,EAAE,GAC/D,EACF,CAAC;;;ACTD,MAAa,OAAO,OAAO,OAAO;CAEhC,aAAA;CAEA,MAAA;CAEA,SAAA;AACF,CAAC"}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/config.ts","../src/meta.ts"],"sourcesContent":["import { defineConfig, type UserConfig } from '@w5s/managed-script';\nimport path from 'node:path';\n\nconst configDir = new URL('.', import.meta.url).pathname;\n\nexport const config: UserConfig = defineConfig({\n scripts: {\n 'prepare': {\n args: [path.join(configDir, 'script', 'prepare.js')],\n runner: 'node',\n },\n 'prepare:gitmodules': {\n args: [path.join(configDir, 'script', 'prepare', 'gitmodules.js')],\n runner: 'node',\n },\n 'prepare:lefthook': {\n args: [path.join(configDir, 'script', 'prepare', 'lefthook.js')],\n runner: 'node',\n },\n 'prepare:skills': {\n args: [path.join(configDir, 'script', 'prepare', 'skills.js')],\n runner: 'node',\n },\n 'rescue': {\n args: [path.join(configDir, 'script', 'rescue.js')],\n runner: 'node',\n },\n },\n});\n","export const meta = Object.freeze({\n // @ts-ignore - these variables are injected at build time\n buildNumber: (typeof __PACKAGE_BUILD_NUMBER__ === 'undefined' ? 0 : __PACKAGE_BUILD_NUMBER__) as number,\n // @ts-ignore - these variables are injected at build time\n name: (typeof __PACKAGE_NAME__ === 'undefined' ? '' : __PACKAGE_NAME__) as string,\n // @ts-ignore - these variables are injected at build time\n version: (typeof __PACKAGE_VERSION__ === 'undefined' ? '' : __PACKAGE_VERSION__) as string,\n});\n"],"mappings":";;;AAGA,MAAM,YAAY,IAAI,IAAI,KAAK,YAAY,GAAG,CAAC,CAAC;AAEhD,MAAa,SAAqB,aAAa,EAC7C,SAAS;CACP,WAAW;EACT,MAAM,CAAC,KAAK,KAAK,WAAW,UAAU,YAAY,CAAC;EACnD,QAAQ;CACV;CACA,sBAAsB;EACpB,MAAM,CAAC,KAAK,KAAK,WAAW,UAAU,WAAW,eAAe,CAAC;EACjE,QAAQ;CACV;CACA,oBAAoB;EAClB,MAAM,CAAC,KAAK,KAAK,WAAW,UAAU,WAAW,aAAa,CAAC;EAC/D,QAAQ;CACV;CACA,kBAAkB;EAChB,MAAM,CAAC,KAAK,KAAK,WAAW,UAAU,WAAW,WAAW,CAAC;EAC7D,QAAQ;CACV;CACA,UAAU;EACR,MAAM,CAAC,KAAK,KAAK,WAAW,UAAU,WAAW,CAAC;EAClD,QAAQ;CACV;AACF,EACF,CAAC;;AC5BD,MAAa,OAAO,OAAO,OAAO;CAEhC,aAAA;CAEA,MAAA;CAEA,SAAA;AACF,CAAC"}
@@ -0,0 +1,6 @@
1
+ import { existsSync, statSync } from "node:fs";
2
+ // src/internal/isGit.ts
3
+ const isGit = existsSync(".git") && statSync(".git").isDirectory();
4
+ export { isGit as t };
5
+
6
+ //# sourceMappingURL=isGit-B965siHE.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"isGit-B965siHE.js","names":[],"sources":["../src/internal/isGit.ts"],"sourcesContent":["import { existsSync, statSync } from 'node:fs';\n\nexport const isGit = existsSync('.git') && statSync('.git').isDirectory();\n"],"mappings":";;AAEA,MAAa,QAAQ,WAAW,MAAM,KAAK,SAAS,MAAM,CAAC,CAAC,YAAY"}
@@ -0,0 +1,28 @@
1
+ import { detect, getUserAgent, resolveCommand as npmCommand } from "package-manager-detector";
2
+ // src/internal/detectAgent.ts
3
+ const defaultAgent = "npm";
4
+ /**
5
+ * Detects the package manager used by the current project.
6
+ *
7
+ * Detection order:
8
+ * 1. The `npm_config_user_agent` environment variable, set by the package manager that is
9
+ * actually invoking the current process — it's guaranteed to be installed and runnable.
10
+ * 2. Lockfile presence (`pnpm-lock.yaml`, `yarn.lock`, ...) or the `package.json`
11
+ * `packageManager` / `devEngines.packageManager` field, used when the current process
12
+ * wasn't spawned by a package manager (e.g. direct `node` invocation).
13
+ * 3. `npm`, as a last resort fallback.
14
+ */
15
+ async function detectAgent() {
16
+ const userAgent = getUserAgent();
17
+ if (userAgent != null) return userAgent;
18
+ return (await detect())?.agent ?? defaultAgent;
19
+ }
20
+ // src/internal/npmCommandOrThrow.ts
21
+ function npmCommandOrThrow(agent, command, args, options) {
22
+ const resolved = npmCommand(agent, command, args, options);
23
+ if (resolved == null) throw new Error(`Could not resolve "${command}" command for package manager "${agent}"`);
24
+ return resolved;
25
+ }
26
+ export { detectAgent as n, npmCommandOrThrow as t };
27
+
28
+ //# sourceMappingURL=npmCommandOrThrow-D7r_D9iV.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"npmCommandOrThrow-D7r_D9iV.js","names":[],"sources":["../src/internal/detectAgent.ts","../src/internal/npmCommandOrThrow.ts"],"sourcesContent":["import type { Agent } from 'package-manager-detector';\n\nimport { detect, getUserAgent } from 'package-manager-detector';\n\nconst defaultAgent: Agent = 'npm';\n\n/**\n * Detects the package manager used by the current project.\n *\n * Detection order:\n * 1. The `npm_config_user_agent` environment variable, set by the package manager that is\n * actually invoking the current process — it's guaranteed to be installed and runnable.\n * 2. Lockfile presence (`pnpm-lock.yaml`, `yarn.lock`, ...) or the `package.json`\n * `packageManager` / `devEngines.packageManager` field, used when the current process\n * wasn't spawned by a package manager (e.g. direct `node` invocation).\n * 3. `npm`, as a last resort fallback.\n */\nexport async function detectAgent(): Promise<Agent> {\n const userAgent = getUserAgent();\n if (userAgent != null) {\n return userAgent;\n }\n const detected = await detect();\n return detected?.agent ?? defaultAgent;\n}\n","import { type Agent, type Command, type ResolveCommandOptions, type ResolvedCommand } from 'package-manager-detector';\n\nimport { npmCommand } from './npmCommand.js';\n\nexport function npmCommandOrThrow(agent: Agent, command: Command, args: Array<string>, options?: ResolveCommandOptions): ResolvedCommand {\n const resolved = npmCommand(agent, command, args, options);\n if (resolved == null) {\n throw new Error(`Could not resolve \"${command}\" command for package manager \"${agent}\"`);\n }\n return resolved;\n}\n"],"mappings":";;AAIA,MAAM,eAAsB;;;;;;;;;;;;AAa5B,eAAsB,cAA8B;CAClD,MAAM,YAAY,aAAa;CAC/B,IAAI,aAAa,MACf,OAAO;CAGT,QAAO,MADgB,OAAO,EAAA,EACb,SAAS;AAC5B;;ACpBA,SAAgB,kBAAkB,OAAc,SAAkB,MAAqB,SAAkD;CACvI,MAAM,WAAW,WAAW,OAAO,SAAS,MAAM,OAAO;CACzD,IAAI,YAAY,MACd,MAAM,IAAI,MAAM,sBAAsB,QAAQ,iCAAiC,MAAM,EAAE;CAEzF,OAAO;AACT"}
@@ -0,0 +1,27 @@
1
+ import { n as detectAgent, t as npmCommandOrThrow } from "./npmCommandOrThrow-D7r_D9iV.js";
2
+ // src/internal/npmExec.ts
3
+ /**
4
+ * Runs a locally installed binary using the detected package manager "exec" command
5
+ * (e.g. `pnpm exec`, `npm exec` / `npx`, `yarn exec`, `bun x`...), in a portable way.
6
+ *
7
+ * @param shell zx shell used to run the resolved command, e.g. the shared `$` from
8
+ * `@w5s/managed-script/zx`, or a preset (e.g. `$({ nothrow: true })`) to customize error handling.
9
+ * @returns a function that runs the given `command` (and its `args`) via the resolved
10
+ * "exec" invocation for the detected package manager.
11
+ * @example
12
+ * ```ts
13
+ * import { $ } from '@w5s/managed-script/zx';
14
+ * import { npmExec } from '../internal/npmExec.js';
15
+ * await npmExec($)('lefthook', ['install']);
16
+ * ```
17
+ */
18
+ function npmExec(shell) {
19
+ return async (command, args) => {
20
+ const agent = await detectAgent();
21
+ const resolved = npmCommandOrThrow(agent, "execute-local", [command, ...args]);
22
+ return shell`${resolved.command} ${resolved.args}`;
23
+ };
24
+ }
25
+ export { npmExec as t };
26
+
27
+ //# sourceMappingURL=npmExec-B2YFcpLE.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"npmExec-B2YFcpLE.js","names":[],"sources":["../src/internal/npmExec.ts"],"sourcesContent":["import type { ZXShell } from '@w5s/managed-script/zx';\n\nimport { detectAgent } from './detectAgent.js';\nimport { npmCommandOrThrow } from './npmCommandOrThrow.js';\n\n/**\n * Runs a locally installed binary using the detected package manager \"exec\" command\n * (e.g. `pnpm exec`, `npm exec` / `npx`, `yarn exec`, `bun x`...), in a portable way.\n *\n * @param shell zx shell used to run the resolved command, e.g. the shared `$` from\n * `@w5s/managed-script/zx`, or a preset (e.g. `$({ nothrow: true })`) to customize error handling.\n * @returns a function that runs the given `command` (and its `args`) via the resolved\n * \"exec\" invocation for the detected package manager.\n * @example\n * ```ts\n * import { $ } from '@w5s/managed-script/zx';\n * import { npmExec } from '../internal/npmExec.js';\n * await npmExec($)('lefthook', ['install']);\n * ```\n */\nexport function npmExec(shell: ZXShell) {\n return async (command: string, args: ReadonlyArray<string>) => {\n const agent = await detectAgent();\n const resolved = npmCommandOrThrow(agent, 'execute-local', [command, ...args]);\n\n return shell`${resolved.command} ${resolved.args}`;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAoBA,SAAgB,QAAQ,OAAgB;CACtC,OAAO,OAAO,SAAiB,SAAgC;EAC7D,MAAM,QAAQ,MAAM,YAAY;EAChC,MAAM,WAAW,kBAAkB,OAAO,iBAAiB,CAAC,SAAS,GAAG,IAAI,CAAC;EAE7E,OAAO,KAAK,GAAG,SAAS,QAAQ,GAAG,SAAS;CAC9C;AACF"}
@@ -0,0 +1 @@
1
+ export declare function main(): Promise<void>;
@@ -0,0 +1,13 @@
1
+ import { $ } from "@w5s/managed-script/zx";
2
+ import { existsSync } from "node:fs";
3
+ import { t as isGit } from "../../isGit-B965siHE.js";
4
+ // src/script/prepare/gitmodules.ts
5
+ async function main() {
6
+ if (!(isGit && existsSync(".gitmodules"))) return;
7
+ await $`git submodule sync --recursive`;
8
+ await $`git submodule update --init --recursive`;
9
+ }
10
+ if (import.meta.main) await main();
11
+ export { main };
12
+
13
+ //# sourceMappingURL=gitmodules.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gitmodules.js","names":[],"sources":["../../../src/script/prepare/gitmodules.ts"],"sourcesContent":["import { $ } from '@w5s/managed-script/zx';\nimport { existsSync } from 'node:fs';\n\nimport { isGit } from '../../internal/isGit.js';\n\nexport async function main() {\n if (!(isGit && existsSync('.gitmodules'))) {\n return;\n }\n\n await $`git submodule sync --recursive`;\n await $`git submodule update --init --recursive`;\n}\n\nif (import.meta.main) await main();\n"],"mappings":";;;;AAKA,eAAsB,OAAO;CAC3B,IAAI,EAAE,SAAS,WAAW,aAAa,IACrC;CAGF,MAAM,CAAC;CACP,MAAM,CAAC;AACT;AAEA,IAAI,YAAY,MAAM,MAAM,KAAK"}
@@ -0,0 +1 @@
1
+ export declare function main(): Promise<void>;
@@ -0,0 +1,11 @@
1
+ import { $, isCI } from "@w5s/managed-script/zx";
2
+ import { t as isGit } from "../../isGit-B965siHE.js";
3
+ import { t as npmExec } from "../../npmExec-B2YFcpLE.js";
4
+ // src/script/prepare/lefthook.ts
5
+ async function main() {
6
+ if (!isCI() && isGit) await npmExec($)("lefthook", ["install"]);
7
+ }
8
+ if (import.meta.main) await main();
9
+ export { main };
10
+
11
+ //# sourceMappingURL=lefthook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"lefthook.js","names":[],"sources":["../../../src/script/prepare/lefthook.ts"],"sourcesContent":["import { $, isCI } from '@w5s/managed-script/zx';\n\nimport { isGit } from '../../internal/isGit.js';\nimport { npmExec } from '../../internal/npmExec.js';\n\nexport async function main() {\n if (!isCI() && isGit) {\n await npmExec($)('lefthook', ['install']);\n }\n}\n\nif (import.meta.main) await main();\n"],"mappings":";;;;AAKA,eAAsB,OAAO;CAC3B,IAAI,CAAC,KAAK,KAAK,OACb,MAAM,QAAQ,CAAC,CAAC,CAAC,YAAY,CAAC,SAAS,CAAC;AAE5C;AAEA,IAAI,YAAY,MAAM,MAAM,KAAK"}
@@ -0,0 +1 @@
1
+ export declare function main(): Promise<void>;
@@ -0,0 +1,67 @@
1
+ import { $, isCI } from "@w5s/managed-script/zx";
2
+ import { existsSync } from "node:fs";
3
+ import { t as npmExec } from "../../npmExec-B2YFcpLE.js";
4
+ import path from "node:path";
5
+ import { access } from "node:fs/promises";
6
+ // src/internal/pathExists.ts
7
+ /**
8
+ * Tells asynchronously if a file or a directory exists.
9
+ *
10
+ * @param target path to check
11
+ * @returns `true` if `target` exists and is accessible, `false` otherwise
12
+ * @example
13
+ * ```ts
14
+ * await pathExists('.agents/skills'); // true
15
+ * await pathExists('missing'); // false
16
+ * ```
17
+ */
18
+ async function pathExists(target) {
19
+ try {
20
+ await access(target);
21
+ return true;
22
+ } catch {
23
+ return false;
24
+ }
25
+ }
26
+ // src/internal/skillsAgentList.ts
27
+ /**
28
+ * Agents supported by `skills experimental_sync` and the project folder where each one reads its skills.
29
+ *
30
+ * - `claude-code` reads `.claude/skills`
31
+ */
32
+ const agentSkillsDirectory = [["claude-code", path.join(".claude", "skills")]];
33
+ /**
34
+ * Returns the agents (as named by the `skills` CLI) whose project skills folder already exists in `cwd`.
35
+ * Folders are checked in parallel, the order of the returned agents is preserved.
36
+ *
37
+ * `skills experimental_sync` takes decisions by itself (detecting installed agents, creating missing folders).
38
+ * Passing only the agents returned here guarantees that skills are installed in existing folders only.
39
+ *
40
+ * @param cwd project root directory
41
+ * @param exists async predicate telling if a path exists (defaults to {@link pathExists})
42
+ * @returns the list of agents to pass to `skills experimental_sync --agent`
43
+ * @example
44
+ * ```ts
45
+ * // .claude/skills exists
46
+ * await skillsAgentList(); // ['claude-code']
47
+ * ```
48
+ */
49
+ async function skillsAgentList(cwd = process.cwd(), exists = pathExists) {
50
+ return (await Promise.all(agentSkillsDirectory.map(async ([agent, directory]) => await exists(path.join(cwd, directory)) ? agent : void 0))).filter((agent) => agent !== void 0);
51
+ }
52
+ // src/script/prepare/skills.ts
53
+ async function main() {
54
+ const isUsingSkills = existsSync("skills-lock.json");
55
+ if (isCI() || !isUsingSkills) return;
56
+ const agentList = ["universal", ...await skillsAgentList()];
57
+ await npmExec($)("skills", [
58
+ "experimental_sync",
59
+ "--yes",
60
+ "--agent",
61
+ ...agentList
62
+ ]);
63
+ }
64
+ if (import.meta.main) await main();
65
+ export { main };
66
+
67
+ //# sourceMappingURL=skills.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"skills.js","names":[],"sources":["../../../src/internal/pathExists.ts","../../../src/internal/skillsAgentList.ts","../../../src/script/prepare/skills.ts"],"sourcesContent":["import { access } from 'node:fs/promises';\n\n/**\n * Tells asynchronously if a file or a directory exists.\n *\n * @param target path to check\n * @returns `true` if `target` exists and is accessible, `false` otherwise\n * @example\n * ```ts\n * await pathExists('.agents/skills'); // true\n * await pathExists('missing'); // false\n * ```\n */\nexport async function pathExists(target: string): Promise<boolean> {\n try {\n await access(target);\n return true;\n } catch {\n return false;\n }\n}\n","import path from 'node:path';\n\nimport { pathExists } from './pathExists.js';\n\n/**\n * Agents supported by `skills experimental_sync` and the project folder where each one reads its skills.\n *\n * - `claude-code` reads `.claude/skills`\n */\nconst agentSkillsDirectory: ReadonlyArray<readonly [agent: string, directory: string]> = [\n ['claude-code', path.join('.claude', 'skills')],\n];\n\n/**\n * Returns the agents (as named by the `skills` CLI) whose project skills folder already exists in `cwd`.\n * Folders are checked in parallel, the order of the returned agents is preserved.\n *\n * `skills experimental_sync` takes decisions by itself (detecting installed agents, creating missing folders).\n * Passing only the agents returned here guarantees that skills are installed in existing folders only.\n *\n * @param cwd project root directory\n * @param exists async predicate telling if a path exists (defaults to {@link pathExists})\n * @returns the list of agents to pass to `skills experimental_sync --agent`\n * @example\n * ```ts\n * // .claude/skills exists\n * await skillsAgentList(); // ['claude-code']\n * ```\n */\nexport async function skillsAgentList(\n cwd: string = process.cwd(),\n exists: (path: string) => Promise<boolean> = pathExists,\n): Promise<Array<string>> {\n const found = await Promise.all(\n agentSkillsDirectory.map(async ([agent, directory]) => ((await exists(path.join(cwd, directory))) ? agent : undefined)),\n );\n\n return found.filter((agent) => agent !== undefined);\n}\n","import { $, isCI } from '@w5s/managed-script/zx';\nimport { existsSync } from 'node:fs';\n\nimport { npmExec } from '../../internal/npmExec.js';\nimport { skillsAgentList } from '../../internal/skillsAgentList.js';\n\nexport async function main() {\n const isUsingSkills = existsSync('skills-lock.json');\n if (isCI() || !isUsingSkills) {\n return;\n }\n\n // Always sync the generic .agents/skills directory; append only specifically detected agents.\n const agentList = ['universal', ...(await skillsAgentList())];\n await npmExec($)('skills', ['experimental_sync', '--yes', '--agent', ...agentList]);\n}\n\nif (import.meta.main) await main();\n"],"mappings":";;;;;;;;;;;;;;;;;AAaA,eAAsB,WAAW,QAAkC;CACjE,IAAI;EACF,MAAM,OAAO,MAAM;EACnB,OAAO;CACT,QAAQ;EACN,OAAO;CACT;AACF;;;;;;;ACXA,MAAM,uBAAmF,CACvF,CAAC,eAAe,KAAK,KAAK,WAAW,QAAQ,CAAC,CAChD;;;;;;;;;;;;;;;;;AAkBA,eAAsB,gBACpB,MAAc,QAAQ,IAAI,GAC1B,SAA6C,YACrB;CAKxB,QAAO,MAJa,QAAQ,IAC1B,qBAAqB,IAAI,OAAO,CAAC,OAAO,eAAiB,MAAM,OAAO,KAAK,KAAK,KAAK,SAAS,CAAC,IAAK,QAAQ,KAAA,CAAU,CACxH,EAAA,CAEa,QAAQ,UAAU,UAAU,KAAA,CAAS;AACpD;;AChCA,eAAsB,OAAO;CAC3B,MAAM,gBAAgB,WAAW,kBAAkB;CACnD,IAAI,KAAK,KAAK,CAAC,eACb;CAIF,MAAM,YAAY,CAAC,aAAa,GAAI,MAAM,gBAAgB,CAAE;CAC5D,MAAM,QAAQ,CAAC,CAAC,CAAC,UAAU;EAAC;EAAqB;EAAS;EAAW,GAAG;CAAS,CAAC;AACpF;AAEA,IAAI,YAAY,MAAM,MAAM,KAAK"}
@@ -0,0 +1 @@
1
+ export declare function main(): Promise<void>;
@@ -0,0 +1,50 @@
1
+ import { $ } from "@w5s/managed-script/zx";
2
+ import npmRunAllTasks from "npm-run-all2";
3
+ import { n as detectAgent, t as npmCommandOrThrow } from "../npmCommandOrThrow-D7r_D9iV.js";
4
+ // src/internal/npmRunAll.ts
5
+ /**
6
+ * Runs every script whose name matches `${scriptPrefix}:*`, in a portable way.
7
+ *
8
+ * `pnpm` natively supports running multiple scripts matching a regex
9
+ * (`pnpm run "/^prefix:.*\/"`), which is used directly. Every other package manager (npm,
10
+ * yarn, bun, ...) has no equivalent built-in feature, so the `npm-run-all2` Node API is used
11
+ * instead to discover and run the matched scripts, via the detected package manager.
12
+ *
13
+ * @param shell zx shell used to run the resolved `pnpm` command. Unused for other package
14
+ * managers, which run through the `npm-run-all2` Node API instead.
15
+ * @returns a function that runs every script matching `${scriptPrefix}:*`.
16
+ * @example
17
+ * ```ts
18
+ * import { $ } from '@w5s/managed-script/zx';
19
+ * import { npmRunAll } from '../internal/npmRunAll.js';
20
+ * await npmRunAll($)('prepare');
21
+ * ```
22
+ */
23
+ function npmRunAll(shell) {
24
+ return async (scriptPrefix) => {
25
+ const agent = await detectAgent();
26
+ switch (agent) {
27
+ case "pnpm":
28
+ await shell`pnpm run "/^${scriptPrefix}:.*/"`;
29
+ return;
30
+ default:
31
+ const { command: npmPath } = npmCommandOrThrow(agent, "agent", []);
32
+ await npmRunAllTasks(`${scriptPrefix}:*`, {
33
+ npmPath,
34
+ parallel: true,
35
+ printLabel: true,
36
+ stderr: process.stderr,
37
+ stdin: process.stdin,
38
+ stdout: process.stdout
39
+ });
40
+ }
41
+ };
42
+ }
43
+ // src/script/prepare.ts
44
+ async function main() {
45
+ await npmRunAll($)("prepare");
46
+ }
47
+ if (import.meta.main) await main();
48
+ export { main };
49
+
50
+ //# sourceMappingURL=prepare.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"prepare.js","names":[],"sources":["../../src/internal/npmRunAll.ts","../../src/script/prepare.ts"],"sourcesContent":["import type { ZXShell } from '@w5s/managed-script/zx';\n\nimport npmRunAllTasks from 'npm-run-all2';\n\nimport { detectAgent } from './detectAgent.js';\nimport { npmCommandOrThrow } from './npmCommandOrThrow.js';\n\n/**\n * Runs every script whose name matches `${scriptPrefix}:*`, in a portable way.\n *\n * `pnpm` natively supports running multiple scripts matching a regex\n * (`pnpm run \"/^prefix:.*\\/\"`), which is used directly. Every other package manager (npm,\n * yarn, bun, ...) has no equivalent built-in feature, so the `npm-run-all2` Node API is used\n * instead to discover and run the matched scripts, via the detected package manager.\n *\n * @param shell zx shell used to run the resolved `pnpm` command. Unused for other package\n * managers, which run through the `npm-run-all2` Node API instead.\n * @returns a function that runs every script matching `${scriptPrefix}:*`.\n * @example\n * ```ts\n * import { $ } from '@w5s/managed-script/zx';\n * import { npmRunAll } from '../internal/npmRunAll.js';\n * await npmRunAll($)('prepare');\n * ```\n */\nexport function npmRunAll(shell: ZXShell) {\n return async (scriptPrefix: string): Promise<void> => {\n const agent = await detectAgent();\n switch (agent) {\n case 'pnpm':\n await shell`pnpm run \"/^${scriptPrefix}:.*/\"`;\n return;\n default:\n // Fallback to npm-run-all2 for other package managers (npm, yarn, bun, ...)\n const { command: npmPath } = npmCommandOrThrow(agent, 'agent', []);\n await npmRunAllTasks(`${scriptPrefix}:*`, {\n npmPath,\n parallel: true,\n printLabel: true,\n stderr: process.stderr,\n stdin: process.stdin,\n stdout: process.stdout,\n });\n }\n };\n}\n","import { $ } from '@w5s/managed-script/zx';\n\nimport { npmRunAll } from '../internal/npmRunAll.js';\n\nexport async function main() {\n await npmRunAll($)('prepare');\n}\n\nif (import.meta.main) await main();\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAyBA,SAAgB,UAAU,OAAgB;CACxC,OAAO,OAAO,iBAAwC;EACpD,MAAM,QAAQ,MAAM,YAAY;EAChC,QAAQ,OAAR;GACE,KAAK;IACH,MAAM,KAAK,eAAe,aAAa;IACvC;GACF;IAEE,MAAM,EAAE,SAAS,YAAY,kBAAkB,OAAO,SAAS,CAAC,CAAC;IACjE,MAAM,eAAe,GAAG,aAAa,KAAK;KACxC;KACA,UAAU;KACV,YAAY;KACZ,QAAQ,QAAQ;KAChB,OAAO,QAAQ;KACf,QAAQ,QAAQ;IAClB,CAAC;EACL;CACF;AACF;;ACzCA,eAAsB,OAAO;CAC3B,MAAM,UAAU,CAAC,CAAC,CAAC,SAAS;AAC9B;AAEA,IAAI,YAAY,MAAM,MAAM,KAAK"}
@@ -1 +1 @@
1
- export {}
1
+ export declare function main(): Promise<void>;
@@ -1,9 +1,35 @@
1
1
  import { $ } from "@w5s/managed-script/zx";
2
- //#region src/script/rescue.ts
3
- const $noThrow = $({ nothrow: true });
4
- await $noThrow`git clean -fdx`;
5
- await $noThrow`pnpm install`;
6
- //#endregion
7
- export {};
2
+ import { t as isGit } from "../isGit-B965siHE.js";
3
+ import { n as detectAgent, t as npmCommandOrThrow } from "../npmCommandOrThrow-D7r_D9iV.js";
4
+ // src/internal/npmInstall.ts
5
+ /**
6
+ * Installs the project dependencies using the detected package manager's "install" command
7
+ * (e.g. `pnpm install`, `npm install`, `yarn install`, `bun install`...), in a portable way.
8
+ *
9
+ * @param shell zx shell used to run the resolved command, e.g. the shared `$` from
10
+ * `@w5s/managed-script/zx`, or a preset (e.g. `$({ nothrow: true })`) to customize error handling.
11
+ * @returns a function that runs the resolved "install" invocation (with extra `args`)
12
+ * for the detected package manager.
13
+ * @example
14
+ * ```ts
15
+ * import { $ } from '@w5s/managed-script/zx';
16
+ * import { npmInstall } from '../internal/npmInstall.js';
17
+ * await npmInstall($)([]);
18
+ * ```
19
+ */
20
+ function npmInstall(shell) {
21
+ return async (args) => {
22
+ const agent = await detectAgent();
23
+ const resolved = npmCommandOrThrow(agent, "install", [...args]);
24
+ return shell`${resolved.command} ${resolved.args}`;
25
+ };
26
+ }
27
+ // src/script/rescue.ts
28
+ async function main() {
29
+ if (isGit) await $({ nothrow: true })`git clean -fdx`;
30
+ await npmInstall($({ nothrow: true }))([]);
31
+ }
32
+ if (import.meta.main) await main();
33
+ export { main };
8
34
 
9
35
  //# sourceMappingURL=rescue.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"rescue.js","names":[],"sources":["../../src/script/rescue.ts"],"sourcesContent":["import { $ } from '@w5s/managed-script/zx';\n\n/* cspell:ignore nothrow */\nconst $noThrow = $({ nothrow: true });\n\nawait $noThrow`git clean -fdx`;\nawait $noThrow`pnpm install`;\n"],"mappings":";;AAGA,MAAM,WAAW,EAAE,EAAE,SAAS,KAAK,CAAC;AAEpC,MAAM,QAAQ;AACd,MAAM,QAAQ"}
1
+ {"version":3,"file":"rescue.js","names":[],"sources":["../../src/internal/npmInstall.ts","../../src/script/rescue.ts"],"sourcesContent":["import type { ZXShell } from '@w5s/managed-script/zx';\n\nimport { detectAgent } from './detectAgent.js';\nimport { npmCommandOrThrow } from './npmCommandOrThrow.js';\n\n/**\n * Installs the project dependencies using the detected package manager's \"install\" command\n * (e.g. `pnpm install`, `npm install`, `yarn install`, `bun install`...), in a portable way.\n *\n * @param shell zx shell used to run the resolved command, e.g. the shared `$` from\n * `@w5s/managed-script/zx`, or a preset (e.g. `$({ nothrow: true })`) to customize error handling.\n * @returns a function that runs the resolved \"install\" invocation (with extra `args`)\n * for the detected package manager.\n * @example\n * ```ts\n * import { $ } from '@w5s/managed-script/zx';\n * import { npmInstall } from '../internal/npmInstall.js';\n * await npmInstall($)([]);\n * ```\n */\nexport function npmInstall(shell: ZXShell) {\n return async (args: ReadonlyArray<string>) => {\n const agent = await detectAgent();\n const resolved = npmCommandOrThrow(agent, 'install', [...args]);\n\n return shell`${resolved.command} ${resolved.args}`;\n };\n}\n","import { $ } from '@w5s/managed-script/zx';\n\nimport { isGit } from '../internal/isGit.js';\nimport { npmInstall } from '../internal/npmInstall.js';\n\n/* cspell:ignore nothrow */\n\nexport async function main() {\n if (isGit) {\n await $({ nothrow: true })`git clean -fdx`;\n }\n await npmInstall($({ nothrow: true }))([]);\n}\n\nif (import.meta.main) await main();\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,WAAW,OAAgB;CACzC,OAAO,OAAO,SAAgC;EAC5C,MAAM,QAAQ,MAAM,YAAY;EAChC,MAAM,WAAW,kBAAkB,OAAO,WAAW,CAAC,GAAG,IAAI,CAAC;EAE9D,OAAO,KAAK,GAAG,SAAS,QAAQ,GAAG,SAAS;CAC9C;AACF;;ACpBA,eAAsB,OAAO;CAC3B,IAAI,OACF,MAAM,EAAE,EAAE,SAAS,KAAK,CAAC,CAAC;CAE5B,MAAM,WAAW,EAAE,EAAE,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC3C;AAEA,IAAI,YAAY,MAAM,MAAM,KAAK"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@w5s/managed-script-config",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.5",
4
4
  "description": "Managed script shared configuration",
5
5
  "keywords": [
6
6
  "config",
@@ -36,13 +36,19 @@
36
36
  "scripts": {
37
37
  "postpack": "clean-package restore"
38
38
  },
39
+ "dependencies": {
40
+ "@w5s/configurator-core": "^1.23.0",
41
+ "npm-run-all2": "^9.0.4",
42
+ "package-manager-detector": "^1.9.0"
43
+ },
39
44
  "peerDependencies": {
40
- "@w5s/managed-script": "workspace:^"
45
+ "@w5s/managed-script": "^1.0.0-alpha.5"
41
46
  },
42
47
  "engines": {
43
48
  "node": ">=22.0.0"
44
49
  },
45
50
  "publishConfig": {
46
51
  "access": "public"
47
- }
52
+ },
53
+ "gitHead": "8c839f967fbc4477af3e767c4ecfa2d8a693eda0"
48
54
  }
package/src/config.ts CHANGED
@@ -1,10 +1,29 @@
1
- import { defineConfig } from '@w5s/managed-script';
1
+ import { defineConfig, type UserConfig } from '@w5s/managed-script';
2
2
  import path from 'node:path';
3
3
 
4
4
  const configDir = new URL('.', import.meta.url).pathname;
5
5
 
6
- export const config = defineConfig({
6
+ export const config: UserConfig = defineConfig({
7
7
  scripts: {
8
- rescue: `node "${path.join(configDir, 'script', 'rescue.js')}"`,
8
+ 'prepare': {
9
+ args: [path.join(configDir, 'script', 'prepare.js')],
10
+ runner: 'node',
11
+ },
12
+ 'prepare:gitmodules': {
13
+ args: [path.join(configDir, 'script', 'prepare', 'gitmodules.js')],
14
+ runner: 'node',
15
+ },
16
+ 'prepare:lefthook': {
17
+ args: [path.join(configDir, 'script', 'prepare', 'lefthook.js')],
18
+ runner: 'node',
19
+ },
20
+ 'prepare:skills': {
21
+ args: [path.join(configDir, 'script', 'prepare', 'skills.js')],
22
+ runner: 'node',
23
+ },
24
+ 'rescue': {
25
+ args: [path.join(configDir, 'script', 'rescue.js')],
26
+ runner: 'node',
27
+ },
9
28
  },
10
29
  });
@@ -0,0 +1,25 @@
1
+ import type { Agent } from 'package-manager-detector';
2
+
3
+ import { detect, getUserAgent } from 'package-manager-detector';
4
+
5
+ const defaultAgent: Agent = 'npm';
6
+
7
+ /**
8
+ * Detects the package manager used by the current project.
9
+ *
10
+ * Detection order:
11
+ * 1. The `npm_config_user_agent` environment variable, set by the package manager that is
12
+ * actually invoking the current process — it's guaranteed to be installed and runnable.
13
+ * 2. Lockfile presence (`pnpm-lock.yaml`, `yarn.lock`, ...) or the `package.json`
14
+ * `packageManager` / `devEngines.packageManager` field, used when the current process
15
+ * wasn't spawned by a package manager (e.g. direct `node` invocation).
16
+ * 3. `npm`, as a last resort fallback.
17
+ */
18
+ export async function detectAgent(): Promise<Agent> {
19
+ const userAgent = getUserAgent();
20
+ if (userAgent != null) {
21
+ return userAgent;
22
+ }
23
+ const detected = await detect();
24
+ return detected?.agent ?? defaultAgent;
25
+ }
@@ -0,0 +1,3 @@
1
+ import { existsSync, statSync } from 'node:fs';
2
+
3
+ export const isGit = existsSync('.git') && statSync('.git').isDirectory();
@@ -0,0 +1 @@
1
+ export { resolveCommand as npmCommand } from 'package-manager-detector';
@@ -0,0 +1,11 @@
1
+ import { type Agent, type Command, type ResolveCommandOptions, type ResolvedCommand } from 'package-manager-detector';
2
+
3
+ import { npmCommand } from './npmCommand.js';
4
+
5
+ export function npmCommandOrThrow(agent: Agent, command: Command, args: Array<string>, options?: ResolveCommandOptions): ResolvedCommand {
6
+ const resolved = npmCommand(agent, command, args, options);
7
+ if (resolved == null) {
8
+ throw new Error(`Could not resolve "${command}" command for package manager "${agent}"`);
9
+ }
10
+ return resolved;
11
+ }
@@ -0,0 +1,28 @@
1
+ import type { ZXShell } from '@w5s/managed-script/zx';
2
+
3
+ import { detectAgent } from './detectAgent.js';
4
+ import { npmCommandOrThrow } from './npmCommandOrThrow.js';
5
+
6
+ /**
7
+ * Runs a locally installed binary using the detected package manager "exec" command
8
+ * (e.g. `pnpm exec`, `npm exec` / `npx`, `yarn exec`, `bun x`...), in a portable way.
9
+ *
10
+ * @param shell zx shell used to run the resolved command, e.g. the shared `$` from
11
+ * `@w5s/managed-script/zx`, or a preset (e.g. `$({ nothrow: true })`) to customize error handling.
12
+ * @returns a function that runs the given `command` (and its `args`) via the resolved
13
+ * "exec" invocation for the detected package manager.
14
+ * @example
15
+ * ```ts
16
+ * import { $ } from '@w5s/managed-script/zx';
17
+ * import { npmExec } from '../internal/npmExec.js';
18
+ * await npmExec($)('lefthook', ['install']);
19
+ * ```
20
+ */
21
+ export function npmExec(shell: ZXShell) {
22
+ return async (command: string, args: ReadonlyArray<string>) => {
23
+ const agent = await detectAgent();
24
+ const resolved = npmCommandOrThrow(agent, 'execute-local', [command, ...args]);
25
+
26
+ return shell`${resolved.command} ${resolved.args}`;
27
+ };
28
+ }
@@ -0,0 +1,28 @@
1
+ import type { ZXShell } from '@w5s/managed-script/zx';
2
+
3
+ import { detectAgent } from './detectAgent.js';
4
+ import { npmCommandOrThrow } from './npmCommandOrThrow.js';
5
+
6
+ /**
7
+ * Installs the project dependencies using the detected package manager's "install" command
8
+ * (e.g. `pnpm install`, `npm install`, `yarn install`, `bun install`...), in a portable way.
9
+ *
10
+ * @param shell zx shell used to run the resolved command, e.g. the shared `$` from
11
+ * `@w5s/managed-script/zx`, or a preset (e.g. `$({ nothrow: true })`) to customize error handling.
12
+ * @returns a function that runs the resolved "install" invocation (with extra `args`)
13
+ * for the detected package manager.
14
+ * @example
15
+ * ```ts
16
+ * import { $ } from '@w5s/managed-script/zx';
17
+ * import { npmInstall } from '../internal/npmInstall.js';
18
+ * await npmInstall($)([]);
19
+ * ```
20
+ */
21
+ export function npmInstall(shell: ZXShell) {
22
+ return async (args: ReadonlyArray<string>) => {
23
+ const agent = await detectAgent();
24
+ const resolved = npmCommandOrThrow(agent, 'install', [...args]);
25
+
26
+ return shell`${resolved.command} ${resolved.args}`;
27
+ };
28
+ }
@@ -0,0 +1,28 @@
1
+ import type { ZXShell } from '@w5s/managed-script/zx';
2
+
3
+ import { detectAgent } from './detectAgent.js';
4
+ import { npmCommandOrThrow } from './npmCommandOrThrow.js';
5
+
6
+ /**
7
+ * Runs a locally installed binary using the detected package manager "exec" command
8
+ * (e.g. `pnpm exec`, `npm exec` / `npx`, `yarn exec`, `bun x`...), in a portable way.
9
+ *
10
+ * @param shell zx shell used to run the resolved command, e.g. the shared `$` from
11
+ * `@w5s/managed-script/zx`, or a preset (e.g. `$({ nothrow: true })`) to customize error handling.
12
+ * @returns a function that runs the given `command` (and its `args`) via the resolved
13
+ * "exec" invocation for the detected package manager.
14
+ * @example
15
+ * ```ts
16
+ * import { $ } from '@w5s/managed-script/zx';
17
+ * import { npmRun } from '../internal/npmRun.js';
18
+ * await npmRun($)('my-script');
19
+ * ```
20
+ */
21
+ export function npmRun(shell: ZXShell) {
22
+ return async (command: string, args: ReadonlyArray<string>) => {
23
+ const agent = await detectAgent();
24
+ const resolved = npmCommandOrThrow(agent, 'run', [command, ...args]);
25
+
26
+ return shell`${resolved.command} ${resolved.args}`;
27
+ };
28
+ }
@@ -0,0 +1,46 @@
1
+ import type { ZXShell } from '@w5s/managed-script/zx';
2
+
3
+ import npmRunAllTasks from 'npm-run-all2';
4
+
5
+ import { detectAgent } from './detectAgent.js';
6
+ import { npmCommandOrThrow } from './npmCommandOrThrow.js';
7
+
8
+ /**
9
+ * Runs every script whose name matches `${scriptPrefix}:*`, in a portable way.
10
+ *
11
+ * `pnpm` natively supports running multiple scripts matching a regex
12
+ * (`pnpm run "/^prefix:.*\/"`), which is used directly. Every other package manager (npm,
13
+ * yarn, bun, ...) has no equivalent built-in feature, so the `npm-run-all2` Node API is used
14
+ * instead to discover and run the matched scripts, via the detected package manager.
15
+ *
16
+ * @param shell zx shell used to run the resolved `pnpm` command. Unused for other package
17
+ * managers, which run through the `npm-run-all2` Node API instead.
18
+ * @returns a function that runs every script matching `${scriptPrefix}:*`.
19
+ * @example
20
+ * ```ts
21
+ * import { $ } from '@w5s/managed-script/zx';
22
+ * import { npmRunAll } from '../internal/npmRunAll.js';
23
+ * await npmRunAll($)('prepare');
24
+ * ```
25
+ */
26
+ export function npmRunAll(shell: ZXShell) {
27
+ return async (scriptPrefix: string): Promise<void> => {
28
+ const agent = await detectAgent();
29
+ switch (agent) {
30
+ case 'pnpm':
31
+ await shell`pnpm run "/^${scriptPrefix}:.*/"`;
32
+ return;
33
+ default:
34
+ // Fallback to npm-run-all2 for other package managers (npm, yarn, bun, ...)
35
+ const { command: npmPath } = npmCommandOrThrow(agent, 'agent', []);
36
+ await npmRunAllTasks(`${scriptPrefix}:*`, {
37
+ npmPath,
38
+ parallel: true,
39
+ printLabel: true,
40
+ stderr: process.stderr,
41
+ stdin: process.stdin,
42
+ stdout: process.stdout,
43
+ });
44
+ }
45
+ };
46
+ }
@@ -0,0 +1,21 @@
1
+ import { access } from 'node:fs/promises';
2
+
3
+ /**
4
+ * Tells asynchronously if a file or a directory exists.
5
+ *
6
+ * @param target path to check
7
+ * @returns `true` if `target` exists and is accessible, `false` otherwise
8
+ * @example
9
+ * ```ts
10
+ * await pathExists('.agents/skills'); // true
11
+ * await pathExists('missing'); // false
12
+ * ```
13
+ */
14
+ export async function pathExists(target: string): Promise<boolean> {
15
+ try {
16
+ await access(target);
17
+ return true;
18
+ } catch {
19
+ return false;
20
+ }
21
+ }
@@ -0,0 +1,39 @@
1
+ import path from 'node:path';
2
+
3
+ import { pathExists } from './pathExists.js';
4
+
5
+ /**
6
+ * Agents supported by `skills experimental_sync` and the project folder where each one reads its skills.
7
+ *
8
+ * - `claude-code` reads `.claude/skills`
9
+ */
10
+ const agentSkillsDirectory: ReadonlyArray<readonly [agent: string, directory: string]> = [
11
+ ['claude-code', path.join('.claude', 'skills')],
12
+ ];
13
+
14
+ /**
15
+ * Returns the agents (as named by the `skills` CLI) whose project skills folder already exists in `cwd`.
16
+ * Folders are checked in parallel, the order of the returned agents is preserved.
17
+ *
18
+ * `skills experimental_sync` takes decisions by itself (detecting installed agents, creating missing folders).
19
+ * Passing only the agents returned here guarantees that skills are installed in existing folders only.
20
+ *
21
+ * @param cwd project root directory
22
+ * @param exists async predicate telling if a path exists (defaults to {@link pathExists})
23
+ * @returns the list of agents to pass to `skills experimental_sync --agent`
24
+ * @example
25
+ * ```ts
26
+ * // .claude/skills exists
27
+ * await skillsAgentList(); // ['claude-code']
28
+ * ```
29
+ */
30
+ export async function skillsAgentList(
31
+ cwd: string = process.cwd(),
32
+ exists: (path: string) => Promise<boolean> = pathExists,
33
+ ): Promise<Array<string>> {
34
+ const found = await Promise.all(
35
+ agentSkillsDirectory.map(async ([agent, directory]) => ((await exists(path.join(cwd, directory))) ? agent : undefined)),
36
+ );
37
+
38
+ return found.filter((agent) => agent !== undefined);
39
+ }
@@ -0,0 +1,15 @@
1
+ import { $ } from '@w5s/managed-script/zx';
2
+ import { existsSync } from 'node:fs';
3
+
4
+ import { isGit } from '../../internal/isGit.js';
5
+
6
+ export async function main() {
7
+ if (!(isGit && existsSync('.gitmodules'))) {
8
+ return;
9
+ }
10
+
11
+ await $`git submodule sync --recursive`;
12
+ await $`git submodule update --init --recursive`;
13
+ }
14
+
15
+ if (import.meta.main) await main();
@@ -0,0 +1,12 @@
1
+ import { $, isCI } from '@w5s/managed-script/zx';
2
+
3
+ import { isGit } from '../../internal/isGit.js';
4
+ import { npmExec } from '../../internal/npmExec.js';
5
+
6
+ export async function main() {
7
+ if (!isCI() && isGit) {
8
+ await npmExec($)('lefthook', ['install']);
9
+ }
10
+ }
11
+
12
+ if (import.meta.main) await main();
@@ -0,0 +1,18 @@
1
+ import { $, isCI } from '@w5s/managed-script/zx';
2
+ import { existsSync } from 'node:fs';
3
+
4
+ import { npmExec } from '../../internal/npmExec.js';
5
+ import { skillsAgentList } from '../../internal/skillsAgentList.js';
6
+
7
+ export async function main() {
8
+ const isUsingSkills = existsSync('skills-lock.json');
9
+ if (isCI() || !isUsingSkills) {
10
+ return;
11
+ }
12
+
13
+ // Always sync the generic .agents/skills directory; append only specifically detected agents.
14
+ const agentList = ['universal', ...(await skillsAgentList())];
15
+ await npmExec($)('skills', ['experimental_sync', '--yes', '--agent', ...agentList]);
16
+ }
17
+
18
+ if (import.meta.main) await main();
@@ -0,0 +1,9 @@
1
+ import { $ } from '@w5s/managed-script/zx';
2
+
3
+ import { npmRunAll } from '../internal/npmRunAll.js';
4
+
5
+ export async function main() {
6
+ await npmRunAll($)('prepare');
7
+ }
8
+
9
+ if (import.meta.main) await main();
@@ -1,7 +1,15 @@
1
1
  import { $ } from '@w5s/managed-script/zx';
2
2
 
3
+ import { isGit } from '../internal/isGit.js';
4
+ import { npmInstall } from '../internal/npmInstall.js';
5
+
3
6
  /* cspell:ignore nothrow */
4
- const $noThrow = $({ nothrow: true });
5
7
 
6
- await $noThrow`git clean -fdx`;
7
- await $noThrow`pnpm install`;
8
+ export async function main() {
9
+ if (isGit) {
10
+ await $({ nothrow: true })`git clean -fdx`;
11
+ }
12
+ await npmInstall($({ nothrow: true }))([]);
13
+ }
14
+
15
+ if (import.meta.main) await main();