t1kit 0.0.0-stage → 0.1.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/README.md +73 -3
- package/bin/t1kit.mjs +9 -0
- package/migrate-semantic-theme-ts/README.md +15 -0
- package/migrate-semantic-theme-ts/index.mjs +15 -0
- package/package.json +31 -6
- package/src/cli.mjs +35 -0
- package/src/commands/index.mjs +6 -0
- package/src/commands/semantic-migrate/README.md +48 -0
- package/src/commands/semantic-migrate/command.mjs +214 -0
- package/src/commands/semantic-migrate/files.mjs +21 -0
- package/src/commands/semantic-migrate/host-tests.mjs +118 -0
- package/src/commands/semantic-migrate/submodule.mjs +208 -0
- package/src/commands/semantic-migrate/transforms.mjs +773 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { spawnSync } from 'node:child_process';
|
|
2
|
+
import { existsSync, renameSync, rmSync, writeFileSync } from 'node:fs';
|
|
3
|
+
import { join, relative, resolve } from 'node:path';
|
|
4
|
+
import { readIfExists } from './files.mjs';
|
|
5
|
+
import {
|
|
6
|
+
SEMANTIC_SUBMODULE_PATH,
|
|
7
|
+
SEMANTIC_THEME_BRANCH,
|
|
8
|
+
SEMANTIC_THEME_TS,
|
|
9
|
+
ensureSubmoduleBranch,
|
|
10
|
+
isLegacyThemeRemoteUrl,
|
|
11
|
+
isThemeTsRemoteUrl,
|
|
12
|
+
parseSemanticSubmodule,
|
|
13
|
+
semanticAddUrl,
|
|
14
|
+
themeGitUnsafeReasons,
|
|
15
|
+
} from './transforms.mjs';
|
|
16
|
+
|
|
17
|
+
const runGit = (root, args, { allowFail = false, inherit = true } = {}) => {
|
|
18
|
+
const result = spawnSync('git', args, {
|
|
19
|
+
cwd: root,
|
|
20
|
+
encoding: 'utf8',
|
|
21
|
+
stdio: inherit ? 'inherit' : ['ignore', 'pipe', 'pipe'],
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
if (!allowFail && result.status !== 0) {
|
|
25
|
+
throw new Error(`git ${args.join(' ')} failed (exit ${result.status ?? 'spawn'})`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return result;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const gitText = (root, args) => (runGit(root, args, { inherit: false, allowFail: true }).stdout || '').trim();
|
|
32
|
+
|
|
33
|
+
const moduleGitDirs = (root, names) => {
|
|
34
|
+
const gitDir = gitText(root, ['rev-parse', '--git-dir']);
|
|
35
|
+
|
|
36
|
+
if (!gitDir) return [];
|
|
37
|
+
|
|
38
|
+
const absGitDir = resolve(root, gitDir);
|
|
39
|
+
const dirs = new Set();
|
|
40
|
+
|
|
41
|
+
for (const name of new Set(names.filter(Boolean).map(String))) {
|
|
42
|
+
const dir = resolve(absGitDir, 'modules', ...name.split('/'));
|
|
43
|
+
if (existsSync(dir)) dirs.add(dir);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return [...dirs];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const inspectThemeGit = (root, path, names) => {
|
|
50
|
+
const worktree = join(root, path);
|
|
51
|
+
const gitDirs = moduleGitDirs(root, names);
|
|
52
|
+
const gitInWorktree = existsSync(worktree) && existsSync(join(worktree, '.git'));
|
|
53
|
+
|
|
54
|
+
const status = gitInWorktree ? gitText(worktree, ['status', '--porcelain']) : '';
|
|
55
|
+
const stashList = [
|
|
56
|
+
gitInWorktree ? gitText(worktree, ['stash', 'list']) : '',
|
|
57
|
+
...gitDirs.map(dir => gitText(root, ['--git-dir', dir, 'stash', 'list'])),
|
|
58
|
+
].join('\n');
|
|
59
|
+
const unpublished = [
|
|
60
|
+
gitInWorktree ? gitText(worktree, ['log', 'HEAD', '--branches', '--not', '--remotes', '--oneline']) : '',
|
|
61
|
+
...gitDirs.map(dir =>
|
|
62
|
+
gitText(root, ['--git-dir', dir, 'log', 'HEAD', '--branches', '--not', '--remotes', '--oneline']),
|
|
63
|
+
),
|
|
64
|
+
].join('\n');
|
|
65
|
+
|
|
66
|
+
return { status, stashList, unpublished, gitDirs };
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const assertThemeGitSafe = (root, path, names) => {
|
|
70
|
+
const { status, stashList, unpublished } = inspectThemeGit(root, path, names);
|
|
71
|
+
const reasons = themeGitUnsafeReasons({ status, stashList, unpublished });
|
|
72
|
+
|
|
73
|
+
if (!reasons.length) return;
|
|
74
|
+
|
|
75
|
+
throw new Error(
|
|
76
|
+
`Не трогаю gitdir темы: ${reasons.join('; ')}.\n` +
|
|
77
|
+
'Закоммитьте и push в front-semantic-theme, затем запустите команду снова. Stash в src/semantic-ui недостаточно — после clone его не будет.',
|
|
78
|
+
);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
const backupGitModuleWorktree = (root, names) => {
|
|
82
|
+
for (const dir of moduleGitDirs(root, names)) {
|
|
83
|
+
let backup = `${dir}.pre-theme-ts`;
|
|
84
|
+
let n = 1;
|
|
85
|
+
|
|
86
|
+
while (existsSync(backup)) {
|
|
87
|
+
n += 1;
|
|
88
|
+
backup = `${dir}.pre-theme-ts-${n}`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
renameSync(dir, backup);
|
|
92
|
+
log(`Старый gitdir темы сохранён: ${relative(root, backup)}`);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const runInitSh = root => {
|
|
97
|
+
const script = join(root, 'init.sh');
|
|
98
|
+
|
|
99
|
+
if (!existsSync(script)) {
|
|
100
|
+
log('init.sh не найден, git submodule init && sync && update --remote');
|
|
101
|
+
runGit(root, ['submodule', 'init']);
|
|
102
|
+
runGit(root, ['submodule', 'sync']);
|
|
103
|
+
runGit(root, ['submodule', 'update', '--remote']);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const result = spawnSync('sh', [script], { cwd: root, encoding: 'utf8', stdio: 'inherit' });
|
|
108
|
+
|
|
109
|
+
if (result.status !== 0) {
|
|
110
|
+
throw new Error(`init.sh failed (exit ${result.status ?? 'spawn'})`);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const themeTsCheckoutLooksReady = root => {
|
|
115
|
+
const dir = join(root, SEMANTIC_SUBMODULE_PATH);
|
|
116
|
+
|
|
117
|
+
return existsSync(join(dir, 'package.json')) || existsSync(join(dir, 'craco.config.ts'));
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const themeOrigins = (root, path, names) => {
|
|
121
|
+
const worktree = join(root, path);
|
|
122
|
+
const urls = [];
|
|
123
|
+
|
|
124
|
+
if (existsSync(worktree) && existsSync(join(worktree, '.git'))) {
|
|
125
|
+
urls.push(gitText(worktree, ['remote', 'get-url', 'origin']));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
for (const dir of moduleGitDirs(root, names)) {
|
|
129
|
+
urls.push(gitText(root, ['--git-dir', dir, 'remote', 'get-url', 'origin']));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return urls.filter(Boolean);
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const alreadyOnThemeTs = (parsed, root, names) => {
|
|
136
|
+
if (!isThemeTsRemoteUrl(parsed?.url) || !themeTsCheckoutLooksReady(root)) return false;
|
|
137
|
+
|
|
138
|
+
const origins = themeOrigins(root, parsed.path || SEMANTIC_SUBMODULE_PATH, names);
|
|
139
|
+
|
|
140
|
+
return !origins.some(isLegacyThemeRemoteUrl);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const semanticTarget = root => {
|
|
144
|
+
const gitmodules = readIfExists(join(root, '.gitmodules')) || '';
|
|
145
|
+
const headGitmodules = gitText(root, ['show', 'HEAD:.gitmodules']);
|
|
146
|
+
const parsed = parseSemanticSubmodule(gitmodules) || parseSemanticSubmodule(headGitmodules);
|
|
147
|
+
const path = parsed?.path || SEMANTIC_SUBMODULE_PATH;
|
|
148
|
+
|
|
149
|
+
return {
|
|
150
|
+
gitmodules,
|
|
151
|
+
parsed,
|
|
152
|
+
path,
|
|
153
|
+
url: semanticAddUrl(gitmodules, headGitmodules),
|
|
154
|
+
themeNames: [path, parsed?.name, 'front-semantic-theme'],
|
|
155
|
+
};
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
export const previewSemanticAddUrl = root => semanticTarget(root).url;
|
|
159
|
+
|
|
160
|
+
export const assertSemanticReplaceSafe = root => {
|
|
161
|
+
const { parsed, path, themeNames } = semanticTarget(root);
|
|
162
|
+
|
|
163
|
+
if (!alreadyOnThemeTs(parsed, root, themeNames)) assertThemeGitSafe(root, path, themeNames);
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export const replaceSemanticSubmodule = root => {
|
|
167
|
+
const { gitmodules, parsed, url, path, themeNames } = semanticTarget(root);
|
|
168
|
+
|
|
169
|
+
log(`\nReplacing submodule ${path} → ${url} (branch ${SEMANTIC_THEME_BRANCH})`);
|
|
170
|
+
|
|
171
|
+
if (alreadyOnThemeTs(parsed, root, themeNames)) {
|
|
172
|
+
log('Уже front-semantic-theme-ts по пути src/semantic-ui, add пропускаю.');
|
|
173
|
+
const next = ensureSubmoduleBranch(gitmodules);
|
|
174
|
+
if (next !== gitmodules) writeFileSync(join(root, '.gitmodules'), next);
|
|
175
|
+
runInitSh(root);
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
runGit(root, ['submodule', 'deinit', '-f', '--', path], { allowFail: true });
|
|
180
|
+
if (parsed?.name && parsed.name !== path) {
|
|
181
|
+
runGit(root, ['submodule', 'deinit', '-f', '--', parsed.name], { allowFail: true });
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
const removed = runGit(root, ['rm', '-rf', '--', path], { allowFail: true });
|
|
185
|
+
|
|
186
|
+
if (removed.status !== 0) {
|
|
187
|
+
runGit(root, ['rm', '-rf', '--cached', '--', path], { allowFail: true });
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const folder = join(root, path);
|
|
191
|
+
if (existsSync(folder)) rmSync(folder, { recursive: true, force: true });
|
|
192
|
+
|
|
193
|
+
backupGitModuleWorktree(root, themeNames);
|
|
194
|
+
|
|
195
|
+
runGit(root, ['submodule', 'add', '-f', url, path]);
|
|
196
|
+
|
|
197
|
+
const afterAdd = readIfExists(join(root, '.gitmodules')) || '';
|
|
198
|
+
const withBranch = ensureSubmoduleBranch(afterAdd);
|
|
199
|
+
|
|
200
|
+
if (withBranch !== afterAdd) {
|
|
201
|
+
writeFileSync(join(root, '.gitmodules'), withBranch);
|
|
202
|
+
runGit(root, ['add', '--', '.gitmodules']);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
runInitSh(root);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const log = message => process.stdout.write(`${message}\n`);
|