@cmdless/gx 0.0.0 → 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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 cmdless
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
@@ -1,45 +1,51 @@
1
1
  # @cmdless/gx
2
2
 
3
- ## ⚠️ IMPORTANT NOTICE ⚠️
3
+ npx for git: runs a package's bin straight from a git repo — a workspace in a monorepo included — cloned, installed and built once per commit, then reused until the ref moves.
4
4
 
5
- **This package is created solely for the purpose of setting up OIDC (OpenID Connect) trusted publishing with npm.**
5
+ ```
6
+ npx @cmdless/gx [options] <repo>[#ref] [args...]
7
+ ```
6
8
 
7
- This is **NOT** a functional package and contains **NO** code or functionality beyond the OIDC setup configuration.
9
+ ```
10
+ npx @cmdless/gx github:owner/repo
11
+ npx @cmdless/gx -w packages/cli github:owner/repo#main --some-flag
12
+ npx @cmdless/gx --build build -w @owner/app github:owner/repo#v1.2.0
13
+ ```
8
14
 
9
- ## Purpose
15
+ - `<repo>` — anything npm installs from git: `github:owner/repo`, `owner/repo`, `git+https://…`, `git+ssh://…`. `#ref` is a branch, tag, commit or `semver:<range>`; the default branch if left out.
16
+ - `-w, --workspace <path or name>` — the package in the repo to run, by its path or its name. The root package if left out.
17
+ - `--build <script>` — a root script to run once for the commit before the bin — its build, say. Repeat it for more; they run in order.
18
+ - `--bin <name>` — which of the package's bins, when it has several and none is named after the package.
10
19
 
11
- This package exists to:
12
- 1. Configure OIDC trusted publishing for the package name `@cmdless/gx`
13
- 2. Enable secure, token-less publishing from CI/CD workflows
14
- 3. Establish provenance for packages published under this name
20
+ Options come before the repo; everything after it goes to the bin, which runs with node in the folder you ran gx in, with its stdio, and its exit code is gx's.
15
21
 
16
- ## What is OIDC Trusted Publishing?
22
+ ## What it does
17
23
 
18
- OIDC trusted publishing allows package maintainers to publish packages directly from their CI/CD workflows without needing to manage npm access tokens. Instead, it uses OpenID Connect to establish trust between the CI/CD provider (like GitHub Actions) and npm.
24
+ 1. Resolves the ref to a commit (`git ls-remote`, through [pacote](https://github.com/npm/pacote), as npm does).
25
+ 2. If that commit isn't in the cache, clones it (shallowly, where the host allows) and installs it with the repo's package manager — pnpm if its `packageManager` field or a `pnpm-lock.yaml` says so, npm otherwise — `npm ci` or `pnpm install --frozen-lockfile` when there's a lockfile. An install runs the root `prepare` script, as installing from git always has.
26
+ 3. Runs each `--build` script that hasn't finished for that commit before.
27
+ 4. Runs the bin.
19
28
 
20
- ## Setup Instructions
29
+ Each commit is cached once, whatever's run from it, in `<temp folder>/gx/<commit>`:
21
30
 
22
- To properly configure OIDC trusted publishing for this package:
31
+ ```
32
+ repo/ the clone, installed - there only once the install finished
33
+ built/<script> each --build script that finished
34
+ lock/ while a run is preparing it; other runs wait
35
+ ```
23
36
 
24
- 1. Go to [npmjs.com](https://www.npmjs.com/) and navigate to your package settings
25
- 2. Configure the trusted publisher (e.g., GitHub Actions)
26
- 3. Specify the repository and workflow that should be allowed to publish
27
- 4. Use the configured workflow to publish your actual package
37
+ The temp folder is cleared by the system, so there's nothing to clean up. If a run fails after the install, gx says where the commit is cached — delete that folder to start it over.
28
38
 
29
- ## DO NOT USE THIS PACKAGE
39
+ Only the ref's lookup needs the network once a commit is cached, and that lookup is every run — it's how gx knows the ref hasn't moved.
30
40
 
31
- This package is a placeholder for OIDC configuration only. It:
32
- - Contains no executable code
33
- - Provides no functionality
34
- - Should not be installed as a dependency
35
- - Exists only for administrative purposes
41
+ ## Monorepos
36
42
 
37
- ## More Information
43
+ With npm workspaces or pnpm, a package's bin is only linked where something depends on it — so gx doesn't use the links: it reads the bin from the package's `package.json` and runs it itself, with the package's and the root's `node_modules/.bin` on `PATH`.
38
44
 
39
- For more details about npm's trusted publishing feature, see:
40
- - [npm Trusted Publishing Documentation](https://docs.npmjs.com/generating-provenance-statements)
41
- - [GitHub Actions OIDC Documentation](https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/about-security-hardening-with-openid-connect)
45
+ A bin that's built needs its build: `--build` with a root script. A script that builds just one app — `"build:cli": "npm run build -w packages/core -w packages/cli"` — keeps the first run short, and other apps from the same commit run their own builds when they're first asked for.
42
46
 
43
- ---
47
+ ## Limits
44
48
 
45
- **Maintained for OIDC setup purposes only**
49
+ - npm and pnpm; a repo whose `packageManager` is yarn is refused.
50
+ - Bins are run with node.
51
+ - Builds of two apps from one commit can clean the same shared output — a `rimraf dist` in a shared package — while the other app runs from it.
package/dist/bin.js ADDED
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env node
2
+ import { spawn } from 'node:child_process';
3
+ import { existsSync, readFileSync } from 'node:fs';
4
+ import { delimiter, join } from 'node:path';
5
+ import { GxError, prepare, resolveCommit, say } from './cache.js';
6
+ import { findBin, findPackage } from './target.js';
7
+ const USAGE = `usage: gx [options] <repo>[#ref] [args...]
8
+
9
+ Runs a package's bin from a git repo at a commit, as npx runs one from the
10
+ registry: cloned, installed and built once per commit, in the temp folder.
11
+
12
+ <repo> github:owner/repo, git+https://..., git+ssh://... -
13
+ anything npm installs from git; #ref is a branch, tag,
14
+ commit or semver:<range>, the default branch if left out
15
+ -w, --workspace the package in the repo, by its path or name - the
16
+ root package if left out
17
+ --build <script> a root script to run once for the commit before the bin,
18
+ such as its build - repeat it for more, run in order
19
+ --bin <name> which of the package's bins, if it has several and none
20
+ is named after it
21
+ -h, --help
22
+ -v, --version`;
23
+ function parse(argv) {
24
+ const builds = [];
25
+ let workspace;
26
+ let bin;
27
+ for (let i = 0; i < argv.length; i++) {
28
+ const arg = argv[i];
29
+ const [flag, inline] = arg.startsWith('--') && arg.includes('=') ? [arg.slice(0, arg.indexOf('=')), arg.slice(arg.indexOf('=') + 1)] : [arg, undefined];
30
+ const value = () => {
31
+ const next = inline ?? argv[++i];
32
+ if (next === undefined)
33
+ throw new GxError(`${flag} takes a value`);
34
+ return next;
35
+ };
36
+ switch (flag) {
37
+ case '-h':
38
+ case '--help': return undefined;
39
+ case '-v':
40
+ case '--version':
41
+ console.log(JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8')).version);
42
+ process.exit(0);
43
+ case '-w':
44
+ case '--workspace':
45
+ workspace = value();
46
+ break;
47
+ case '--build':
48
+ builds.push(value());
49
+ break;
50
+ case '--bin':
51
+ bin = value();
52
+ break;
53
+ case '--': {
54
+ const [spec, ...args] = argv.slice(i + 1);
55
+ return spec === undefined ? undefined : { spec, args, workspace, builds, bin };
56
+ }
57
+ default:
58
+ if (arg.startsWith('-'))
59
+ throw new GxError(`no option ${flag}`);
60
+ return { spec: arg, args: argv.slice(i + 1), workspace, builds, bin };
61
+ }
62
+ }
63
+ return undefined;
64
+ }
65
+ async function main() {
66
+ const options = parse(process.argv.slice(2));
67
+ if (!options) {
68
+ console.error(USAGE);
69
+ return 1;
70
+ }
71
+ const { sha, urls } = await resolveCommit(options.spec);
72
+ const { dir, repo, pm } = await prepare(sha, urls, options.builds);
73
+ try {
74
+ const pkg = await findPackage(repo, pm, options.workspace);
75
+ const bin = await findBin(pkg, options.bin);
76
+ if (!existsSync(bin))
77
+ throw new GxError(`${bin} isn't there - does it need --build?`);
78
+ return await exec(bin, options.args, [join(pkg, 'node_modules', '.bin'), join(repo, 'node_modules', '.bin')]);
79
+ }
80
+ catch (error) {
81
+ if (error instanceof GxError)
82
+ say(`(the commit is cached in ${dir} - delete it to start over)`);
83
+ throw error;
84
+ }
85
+ }
86
+ // the bin, with node, in this folder - its exit is gx's
87
+ function exec(bin, args, paths) {
88
+ const PATH = [...paths, process.env.PATH].filter(Boolean).join(delimiter);
89
+ const child = spawn(process.execPath, [bin, ...args], { stdio: 'inherit', env: { ...process.env, PATH } });
90
+ const forward = (signal) => child.kill(signal);
91
+ // the terminal sends ^C to the bin too - gx waits for it to exit
92
+ const ignore = () => { };
93
+ process.on('SIGINT', ignore);
94
+ process.on('SIGTERM', forward);
95
+ process.on('SIGHUP', forward);
96
+ return new Promise((resolve, reject) => {
97
+ child.on('error', reject);
98
+ child.on('exit', (code, signal) => {
99
+ process.off('SIGINT', ignore);
100
+ process.off('SIGTERM', forward);
101
+ process.off('SIGHUP', forward);
102
+ if (signal)
103
+ process.kill(process.pid, signal);
104
+ resolve(code ?? 1);
105
+ });
106
+ });
107
+ }
108
+ main().then(code => process.exitCode = code, error => {
109
+ say(error instanceof GxError ? error.message : error?.stack ?? String(error));
110
+ process.exitCode = 1;
111
+ });
112
+ //# sourceMappingURL=bin.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bin.js","sourceRoot":"","sources":["../src/bin.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAC5C,OAAO,EA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AK,CAAC,EAAE;IACnD,GAAG,CAAC,KAAK,YAAY,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK,EAAE,KAAK,IAAI,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC;IAC9E,OAAO,CAAC,QAAQ,GAAG,CAAC,CAAC;AACvB,CAAC,CAAC,CAAC"}
package/dist/cache.js ADDED
@@ -0,0 +1,173 @@
1
+ // A repo at a commit, cloned, installed and built once into the temp folder,
2
+ // and shared by every gx run of that commit after.
3
+ //
4
+ // <tmp>/gx/<sha>/
5
+ // repo/ the clone - renamed in only once it's installed
6
+ // built/<script> one for each --build script that has finished
7
+ // lock/ held while anything above is being made
8
+ import { spawn } from 'node:child_process';
9
+ import { existsSync } from 'node:fs';
10
+ import { mkdir, mkdtemp, readdir, readFile, rename, rm, writeFile } from 'node:fs/promises';
11
+ import { tmpdir } from 'node:os';
12
+ import { join } from 'node:path';
13
+ import { setTimeout as sleep } from 'node:timers/promises';
14
+ import git from '@npmcli/git';
15
+ import npa from 'npm-package-arg';
16
+ import pacote from 'pacote';
17
+ export class GxError extends Error {
18
+ }
19
+ // the commit a git spec names now, and where it can be cloned from - a
20
+ // hosted shortcut (github:owner/repo) over https, then ssh, as npm does
21
+ export async function resolveCommit(spec) {
22
+ const parsed = npa(spec);
23
+ if (parsed.type !== 'git')
24
+ throw new GxError(`${spec} isn't a git repo - for a package from the registry, use npx`);
25
+ const resolved = npa(await pacote.resolve(spec));
26
+ const sha = resolved.gitCommittish;
27
+ if (!sha)
28
+ throw new GxError(`couldn't find the commit ${spec} is at`);
29
+ const urls = parsed.hosted?.default === 'shortcut'
30
+ ? [parsed.hosted.https({ noCommittish: true }), parsed.hosted.sshurl({ noCommittish: true })]
31
+ : [resolved.fetchSpec];
32
+ return { sha, urls };
33
+ }
34
+ export const cacheRoot = () => join(tmpdir(), 'gx');
35
+ // The repo at sha, ready to run: cloned and installed if it isn't, and each
36
+ // of builds run if it hasn't been - all while holding the commit's lock, so
37
+ // two runs never make the same thing at once
38
+ export async function prepare(sha, urls, builds) {
39
+ const dir = join(cacheRoot(), sha);
40
+ const repo = join(dir, 'repo');
41
+ const done = (script) => join(dir, 'built', encodeURIComponent(script));
42
+ if (existsSync(repo) && builds.every(script => existsSync(done(script))))
43
+ return { dir, repo, pm: await packageManager(repo) };
44
+ await mkdir(dir, { recursive: true });
45
+ const unlock = await lock(join(dir, 'lock'));
46
+ try {
47
+ if (!existsSync(repo))
48
+ await install(dir, repo, sha, urls);
49
+ const pm = await packageManager(repo);
50
+ for (const script of builds) {
51
+ if (existsSync(done(script)))
52
+ continue;
53
+ say(`running ${script}`);
54
+ await run(pm, ['run', script], repo, `${script} failed`);
55
+ await mkdir(join(dir, 'built'), { recursive: true });
56
+ await writeFile(done(script), '');
57
+ }
58
+ return { dir, repo, pm };
59
+ }
60
+ finally {
61
+ await unlock();
62
+ }
63
+ }
64
+ // clone and install into a folder of its own, then rename it to repo - so
65
+ // repo is only ever there whole
66
+ async function install(dir, repo, sha, urls) {
67
+ for (const entry of await readdir(dir)) {
68
+ if (entry.startsWith('repo.'))
69
+ await rm(join(dir, entry), { recursive: true, force: true });
70
+ }
71
+ const temp = await mkdtemp(join(dir, 'repo.'));
72
+ try {
73
+ say(`cloning ${sha}`);
74
+ await cloneFrom(urls, sha, temp);
75
+ const pm = await packageManager(temp);
76
+ say(`installing with ${pm}`);
77
+ await run(pm, installArgs(pm, temp), temp, 'install failed');
78
+ await rename(temp, repo);
79
+ }
80
+ catch (error) {
81
+ await rm(temp, { recursive: true, force: true });
82
+ throw error;
83
+ }
84
+ }
85
+ async function cloneFrom(urls, sha, target) {
86
+ let last;
87
+ for (const url of urls) {
88
+ try {
89
+ return await git.clone(url, sha, target);
90
+ }
91
+ catch (error) {
92
+ last = error;
93
+ await rm(target, { recursive: true, force: true });
94
+ await mkdir(target);
95
+ }
96
+ }
97
+ throw new GxError(`couldn't clone ${urls.join(' or ')}: ${last?.message ?? last}`);
98
+ }
99
+ // the repo's package manager: what its packageManager field says, or its
100
+ // lockfile's - npm if neither says
101
+ export async function packageManager(repo) {
102
+ const declared = (await readJson(join(repo, 'package.json')))?.packageManager;
103
+ if (typeof declared === 'string') {
104
+ const name = declared.split('@')[0];
105
+ if (name === 'npm' || name === 'pnpm')
106
+ return name;
107
+ throw new GxError(`the repo uses ${name}, and gx runs npm and pnpm`);
108
+ }
109
+ return existsSync(join(repo, 'pnpm-lock.yaml')) ? 'pnpm' : 'npm';
110
+ }
111
+ function installArgs(pm, repo) {
112
+ if (pm === 'pnpm')
113
+ return existsSync(join(repo, 'pnpm-lock.yaml')) ? ['install', '--frozen-lockfile'] : ['install'];
114
+ const locked = existsSync(join(repo, 'package-lock.json')) || existsSync(join(repo, 'npm-shrinkwrap.json'));
115
+ return [locked ? 'ci' : 'install', '--no-audit', '--no-fund'];
116
+ }
117
+ // a package manager's command, its output on stderr so gx's stdout is only
118
+ // ever the bin's
119
+ export function run(pm, args, cwd, failed) {
120
+ return new Promise((resolve, reject) => {
121
+ const child = spawn(pm, args, { cwd, stdio: ['ignore', 2, 2], shell: process.platform === 'win32' });
122
+ child.on('error', error => reject(error.code === 'ENOENT'
123
+ ? new GxError(`${pm} isn't installed`) : error));
124
+ child.on('exit', code => code === 0 ? resolve() : reject(new GxError(failed)));
125
+ });
126
+ }
127
+ // A lock is a folder with its holder's pid in it, renamed into place whole -
128
+ // a rename onto a folder that's there fails, so only one run gets it. One
129
+ // whose holder has died is taken over.
130
+ async function lock(path) {
131
+ let waiting = false;
132
+ for (;;) {
133
+ const mine = await mkdtemp(`${path}.`);
134
+ await writeFile(join(mine, 'pid'), String(process.pid));
135
+ try {
136
+ await rename(mine, path);
137
+ return () => rm(path, { recursive: true, force: true });
138
+ }
139
+ catch (error) {
140
+ await rm(mine, { recursive: true, force: true });
141
+ if (!['ENOTEMPTY', 'EEXIST', 'EPERM'].includes(error.code))
142
+ throw error;
143
+ }
144
+ const pid = Number(await readFile(join(path, 'pid'), 'utf8').catch(() => ''));
145
+ if (pid && !alive(pid)) {
146
+ await rm(path, { recursive: true, force: true });
147
+ continue;
148
+ }
149
+ if (!waiting)
150
+ say(`waiting for another gx (pid ${pid}) to finish preparing`);
151
+ waiting = true;
152
+ await sleep(250);
153
+ }
154
+ }
155
+ function alive(pid) {
156
+ try {
157
+ process.kill(pid, 0);
158
+ return true;
159
+ }
160
+ catch (error) {
161
+ return error.code === 'EPERM';
162
+ }
163
+ }
164
+ export async function readJson(path) {
165
+ try {
166
+ return JSON.parse(await readFile(path, 'utf8'));
167
+ }
168
+ catch {
169
+ return undefined;
170
+ }
171
+ }
172
+ export const say = (message) => process.stderr.write(`gx: ${message}\n`);
173
+ //# sourceMappingURL=cache.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache.js","sourceRoot":"","sources":["../src/cache.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,mDAAmD;AACnD,EAAE;AACF,oBAAoB;AACpB,uEAAuE;AACvE,qEAAqE;AACrE,+DAA+D;AAE/D,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC5F,OAAO,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AACjC,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,UAAU,IAAI,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAC3D,OAAO,GAAG,MAAM,aAAa,CAAC;AAC9B,OAAO,GAAG,MAAM,iBAAiB,CAAC;AAClC,OAAO,MAAM,MAAM,QAAQ,CAAC;AAE5B,MAAM,OAAO,OAAQ,SAAQ,KAAK;CAAI;AAItC,uEAAuE;AACvE,wEAAwE;AACxE,MAAM,CAAC,KAAK,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;IACnB,CAAC;AACH,CAAC;AAED,SAAS,KAAK,CAAC,GAAW;IACxB,IAAI,CAAC;QACH,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;QACrB,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAQ,KAA+B,CAAC,IAAI,KAAK,OAAO,CAAC;IAC3D,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,QAAQ,CAAC,IAAY;IACzC,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAC;IAClD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,MAAM,CAAC,MAAM,GAAG,GAAG,CAAC,OAAe,EAAE,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,OAAO,IAAI,CAAC,CAAC"}
package/dist/target.js ADDED
@@ -0,0 +1,51 @@
1
+ // Which package in the repo, and which of its bins - chosen as npx would.
2
+ import { execFile } from 'node:child_process';
3
+ import { existsSync } from 'node:fs';
4
+ import { join, relative, resolve, isAbsolute } from 'node:path';
5
+ import { promisify } from 'node:util';
6
+ import { GxError, readJson } from './cache.js';
7
+ // the package a -w names, by its path in the repo or its name - the repo's
8
+ // root package without one
9
+ export async function findPackage(repo, pm, workspace) {
10
+ if (workspace === undefined)
11
+ return repo;
12
+ const byPath = resolve(repo, workspace);
13
+ const rel = relative(repo, byPath);
14
+ if (!rel.startsWith('..') && !isAbsolute(rel) && existsSync(join(byPath, 'package.json')))
15
+ return byPath;
16
+ const all = await workspaces(repo, pm);
17
+ const found = all.find(each => each.name === workspace);
18
+ if (!found)
19
+ throw new GxError(`no workspace ${workspace} - there's ${all.map(each => each.name).join(', ') || 'none'}`);
20
+ return found.path;
21
+ }
22
+ async function workspaces(repo, pm) {
23
+ const [command, args] = pm === 'pnpm'
24
+ ? ['pnpm', ['ls', '--recursive', '--depth', '-1', '--json']]
25
+ : ['npm', ['query', '.workspace']];
26
+ const { stdout } = await promisify(execFile)(command, args, { cwd: repo, shell: process.platform === 'win32', maxBuffer: 64 * 1024 * 1024 });
27
+ return JSON.parse(stdout)
28
+ .filter(each => each.name && each.path && resolve(each.path) !== resolve(repo))
29
+ .map(({ name, path }) => ({ name, path }));
30
+ }
31
+ // the file a package's bin runs: its only one, or the one named after it,
32
+ // or the one asked for
33
+ export async function findBin(dir, asked) {
34
+ const pkg = await readJson(join(dir, 'package.json'));
35
+ if (!pkg)
36
+ throw new GxError(`${dir} has no package.json`);
37
+ const name = pkg.name ?? '';
38
+ const unscoped = name.replace(/^@[^/]+\//, '');
39
+ const bins = typeof pkg.bin === 'string' ? { [unscoped]: pkg.bin } : pkg.bin ?? {};
40
+ const names = Object.keys(bins);
41
+ const what = name || 'the root package';
42
+ if (names.length === 0)
43
+ throw new GxError(`${what} has no bin - pick a workspace with -w`);
44
+ const chosen = asked ?? (names.length === 1 ? names[0] : names.includes(unscoped) ? unscoped : undefined);
45
+ if (chosen === undefined)
46
+ throw new GxError(`${what} has bins ${names.join(', ')} - pick one with --bin`);
47
+ if (!(chosen in bins))
48
+ throw new GxError(`${what} has no bin ${chosen} - there's ${names.join(', ')}`);
49
+ return join(dir, bins[chosen]);
50
+ }
51
+ //# sourceMappingURL=target.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"target.js","sourceRoot":"","sources":["../src/target.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAE1E,OAAO,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,O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K,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IAChC,MAAM,IAAI,GAAG,IAAI,IAAI,kBAAkB,CAAC;IACxC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,MAAM,IAAI,OAAO,CAAC,GAAG,IAAI,wCAAwC,CAAC,CAAC;IAC3F,MAAM,MAAM,GAAG,KAAK,IAAI,CAAC,KAAK,CAAC,MAAM,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;IAC1G,IAAI,MAAM,KAAK,SAAS;QAAE,MAAM,IAAI,OAAO,CAAC,GAAG,IAAI,aAAa,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAC;IAC1G,IAAI,CAAC,CAAC,MAAM,IAAI,IAAI,CAAC;QAAE,MAAM,IAAI,OAAO,CAAC,GAAG,IAAI,eAAe,MAAM,cAAc,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACvG,OAAO,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;AACjC,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,43 @@
1
1
  {
2
2
  "name": "@cmdless/gx",
3
- "version": "0.0.0",
4
- "description": "OIDC trusted publishing setup package for @cmdless/gx",
5
- "keywords": [
6
- "oidc",
7
- "trusted-publishing",
8
- "setup"
9
- ]
3
+ "description": "Run a package's bin straight from a git repo, cached by commit - npx or dlx for git, workspaces included",
4
+ "version": "0.0.1",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "author": {
8
+ "name": "Fluttershy",
9
+ "url": "https://github.com/Fluttershy"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/cmdless/gx.git"
14
+ },
15
+ "bugs": {
16
+ "url": "https://github.com/cmdless/gx/issues"
17
+ },
18
+ "homepage": "https://github.com/cmdless/gx#readme",
19
+ "bin": {
20
+ "gx": "dist/bin.js"
21
+ },
22
+ "files": [
23
+ "dist/",
24
+ "LICENSE",
25
+ "README.md"
26
+ ],
27
+ "engines": {
28
+ "node": ">=20"
29
+ },
30
+ "scripts": {
31
+ "build": "tsc",
32
+ "prepare": "tsc"
33
+ },
34
+ "dependencies": {
35
+ "@npmcli/git": "8.0.0",
36
+ "npm-package-arg": "14.0.0",
37
+ "pacote": "22.0.1"
38
+ },
39
+ "devDependencies": {
40
+ "@types/node": "24.19.4",
41
+ "typescript": "7.0.2"
42
+ }
10
43
  }