slicekit 0.2.0 → 0.2.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/package.json +1 -1
- package/src/cli/index.js +11 -0
- package/src/config.js +5 -3
- package/src/git.js +35 -0
- package/src/init.js +3 -2
package/package.json
CHANGED
package/src/cli/index.js
CHANGED
|
@@ -104,5 +104,16 @@ export async function main(argv, io) {
|
|
|
104
104
|
if (first !== undefined) io.stderr(`slicekit: unknown command '${first}'\n`);
|
|
105
105
|
return 2;
|
|
106
106
|
}
|
|
107
|
+
// `<command> --help` (P3/S-36, issue #77) answers like `--help`; one after `--` is the command's.
|
|
108
|
+
const end = rest.indexOf('--');
|
|
109
|
+
if ((end === -1 ? rest : rest.slice(0, end)).includes('--help')) {
|
|
110
|
+
io.stdout(commandUsage(command));
|
|
111
|
+
return 0;
|
|
112
|
+
}
|
|
107
113
|
return command.run(rest, io);
|
|
108
114
|
}
|
|
115
|
+
|
|
116
|
+
function commandUsage(command) {
|
|
117
|
+
const lines = command.usage.split('\n').map((line) => ` slicekit ${line.trim()}`);
|
|
118
|
+
return `usage:\n${lines.join('\n')}\n`;
|
|
119
|
+
}
|
package/src/config.js
CHANGED
|
@@ -10,6 +10,8 @@ export const CONFIG_PATH = path.join('.slicekit', 'config.json');
|
|
|
10
10
|
const COMMAND_KEYS = ['test', 'lint', 'build', 'e2e'];
|
|
11
11
|
const REVIEW_MODES = ['required', 'optional'];
|
|
12
12
|
const BRANCH = /^[A-Za-z0-9][A-Za-z0-9._/-]*$/;
|
|
13
|
+
// A branch name the config accepts as defaultBranch or a protected branch.
|
|
14
|
+
export const isBranchName = (name) => BRANCH.test(name);
|
|
13
15
|
|
|
14
16
|
// Matched case-insensitively: compileDestructive adds the i flag, so `drop table` is caught too.
|
|
15
17
|
// No force-push entry: pre-bash's rule 3 refuses force pushes from the parsed command.
|
|
@@ -35,15 +37,15 @@ export function seedBanned() {
|
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
// init always passes cli (from --cli or the D-30 probe); the default serves callers that do not.
|
|
38
|
-
export function defaultConfig({ name, shape, docsRoot = 'docs', commands = {}, cli = 'slicekit' }) {
|
|
40
|
+
export function defaultConfig({ name, shape, docsRoot = 'docs', commands = {}, cli = 'slicekit', defaultBranch = 'main' }) {
|
|
39
41
|
return {
|
|
40
42
|
configVersion: CONFIG_VERSION,
|
|
41
43
|
name,
|
|
42
44
|
shape,
|
|
43
45
|
docsRoot,
|
|
44
|
-
defaultBranch
|
|
46
|
+
defaultBranch,
|
|
45
47
|
cli,
|
|
46
|
-
protectedBranches: [
|
|
48
|
+
protectedBranches: [defaultBranch],
|
|
47
49
|
commands: { test: '', lint: '', build: '', e2e: '', ...commands },
|
|
48
50
|
repos: [],
|
|
49
51
|
gitHost: { kind: 'github' },
|
package/src/git.js
CHANGED
|
@@ -45,6 +45,41 @@ export function currentBranch(root) {
|
|
|
45
45
|
|
|
46
46
|
export const headSha = (root) => git(root, ['rev-parse', 'HEAD']);
|
|
47
47
|
|
|
48
|
+
// The branch a new config names as its default (P3/S-36, issue #78). In order: the one origin's
|
|
49
|
+
// HEAD points at; else main, master or git's init.defaultBranch, the first that is a local branch;
|
|
50
|
+
// else, in an empty repository (a plain `git init` has no branch yet), the branch HEAD is on when it
|
|
51
|
+
// is one of those three names; else main. main comes before init.defaultBranch because that setting
|
|
52
|
+
// may be the machine's, not this project's (Git for Windows sets master system-wide), while an empty
|
|
53
|
+
// repository's HEAD was named by it. Any other branch HEAD is on is not taken: a branch named the
|
|
54
|
+
// default is protected and refuses commits, so a feature branch guessed wrong would block work,
|
|
55
|
+
// where a wrong main only stops `graph` with a message (issue #87). A name `accept` refuses (one
|
|
56
|
+
// the config would reject) is passed over. Outside a repository there is no branch to read: main.
|
|
57
|
+
export function detectDefaultBranch(root, { accept = () => true } = {}) {
|
|
58
|
+
if (!isRepo(root)) return 'main';
|
|
59
|
+
const read = (args) => {
|
|
60
|
+
try {
|
|
61
|
+
return git(root, args);
|
|
62
|
+
} catch (e) {
|
|
63
|
+
if (e instanceof GitError) return '';
|
|
64
|
+
throw e;
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
const ok = (b) => b !== '' && accept(b);
|
|
68
|
+
const local = (b) => read(['rev-parse', '--verify', '-q', `refs/heads/${b}`]) !== '';
|
|
69
|
+
const empty = !hasCommits(root);
|
|
70
|
+
// origin's HEAD counts only while its branch exists: after the remote renames its default and a
|
|
71
|
+
// pruning fetch drops the old branch, HEAD still names it, and graph could not find it. An empty
|
|
72
|
+
// repository has no branch at all yet, so there its name is taken as it is.
|
|
73
|
+
const origin = read(['symbolic-ref', '-q', 'refs/remotes/origin/HEAD']);
|
|
74
|
+
const fromOrigin = origin.startsWith('refs/remotes/origin/') ? origin.slice('refs/remotes/origin/'.length) : '';
|
|
75
|
+
if (ok(fromOrigin) && (empty || read(['rev-parse', '--verify', '-q', origin]) !== '')) return fromOrigin;
|
|
76
|
+
const named = ['main', 'master', read(['config', '--get', 'init.defaultBranch'])].filter(ok);
|
|
77
|
+
const found = named.find(local);
|
|
78
|
+
if (found) return found;
|
|
79
|
+
const head = empty ? read(['symbolic-ref', '-q', 'HEAD']).replace(/^refs\/heads\//, '') : '';
|
|
80
|
+
return named.includes(head) ? head : 'main';
|
|
81
|
+
}
|
|
82
|
+
|
|
48
83
|
// Whether `sha` names a commit object in the repository at root.
|
|
49
84
|
export function commitExists(root, sha) {
|
|
50
85
|
assertRef(sha);
|
package/src/init.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import fs from 'node:fs';
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { fileURLToPath } from 'node:url';
|
|
4
|
-
import { CONFIG_PATH, ConfigError, defaultConfig, loadConfig, validateConfig } from './config.js';
|
|
4
|
+
import { CONFIG_PATH, ConfigError, defaultConfig, isBranchName, loadConfig, validateConfig } from './config.js';
|
|
5
5
|
import { exists, listFiles, readText, writeText } from './fs.js';
|
|
6
|
-
import { currentBranch, git, GitError, hasCommits, isRepo } from './git.js';
|
|
6
|
+
import { currentBranch, detectDefaultBranch, git, GitError, hasCommits, isRepo } from './git.js';
|
|
7
7
|
import { GITHOOKS, HOOKS_PATH, hooksPathValue, isInstalledHooksPath } from './githooks.js';
|
|
8
8
|
import { probeCli } from './hook-cli.js';
|
|
9
9
|
import { kitPaths } from './paths.js';
|
|
@@ -202,6 +202,7 @@ function resolveConfig(root, { name, shape, docsRoot, commands, cli, env }) {
|
|
|
202
202
|
docsRoot: docsRoot ?? 'docs',
|
|
203
203
|
commands,
|
|
204
204
|
cli: probed.cli,
|
|
205
|
+
defaultBranch: detectDefaultBranch(root, { accept: isBranchName }),
|
|
205
206
|
});
|
|
206
207
|
const errors = validateConfig(cfg);
|
|
207
208
|
if (errors.length > 0) throw new ConfigError(errors);
|