components-differ 1.2.1 → 1.2.3
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 +48 -16
- package/dist/components.js +130 -0
- package/dist/create-diff.js +77 -0
- package/dist/extract-imports.js +90 -0
- package/dist/git.js +117 -0
- package/dist/index.js +136 -0
- package/dist/parse-file-path.js +73 -0
- package/dist/release.js +50 -0
- package/dist/resolve-imports.js +103 -0
- package/package.json +14 -43
- package/src/components.ts +170 -0
- package/src/create-diff.ts +156 -0
- package/src/extract-imports.ts +87 -0
- package/{dist/src/git.mjs → src/git.ts} +45 -27
- package/src/index.ts +173 -0
- package/{dist/src/parse-file-path.mjs → src/parse-file-path.ts} +47 -14
- package/src/release.ts +71 -0
- package/src/resolve-imports.ts +121 -0
- package/tsconfig.json +15 -0
- package/dist/index.mjs +0 -114
- package/dist/src/components.mjs +0 -111
- package/dist/src/create-diff.mjs +0 -96
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Extract npm package names from import/require statements in source code.
|
|
3
|
+
* Used to include only dependencies that are actually used by the registry item files.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Returns the root package name for a specifier. Handles scoped packages and subpaths.
|
|
8
|
+
* - "react" -> "react"
|
|
9
|
+
* - "lodash/merge" -> "lodash"
|
|
10
|
+
* - "@scope/pkg" -> "@scope/pkg"
|
|
11
|
+
* - "@scope/pkg/sub" -> "@scope/pkg"
|
|
12
|
+
*/
|
|
13
|
+
function specifierToPackageName(specifier: string): string | null {
|
|
14
|
+
const s = specifier.trim();
|
|
15
|
+
if (!s) return null;
|
|
16
|
+
// Node / bundler builtins
|
|
17
|
+
if (s.startsWith("node:") || s === "node") return null;
|
|
18
|
+
// Relative imports – not packages
|
|
19
|
+
if (s.startsWith(".") || s.startsWith("/")) return null;
|
|
20
|
+
// Common path aliases (not package names)
|
|
21
|
+
if (s.startsWith("@/") || s.startsWith("~/") || s.startsWith("#")) return null;
|
|
22
|
+
// Scoped package: @scope/pkg or @scope/pkg/subpath
|
|
23
|
+
if (s.startsWith("@")) {
|
|
24
|
+
const firstSlash = s.indexOf("/");
|
|
25
|
+
if (firstSlash === -1) return s;
|
|
26
|
+
const secondSlash = s.indexOf("/", firstSlash + 1);
|
|
27
|
+
return secondSlash === -1 ? s : s.slice(0, secondSlash);
|
|
28
|
+
}
|
|
29
|
+
// Normal package or subpath
|
|
30
|
+
const slash = s.indexOf("/");
|
|
31
|
+
return slash === -1 ? s : s.slice(0, slash);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const RE_IMPORT =
|
|
35
|
+
/(?:import\s+(?:(?:\{[^}]*\}|\*\s+as\s+\w+|\w+)\s+from\s+)?|import\s*)['"]([^'"]+)['"]|require\s*\(\s*['"]([^'"]+)['"]\s*\)|import\s*\(\s*['"]([^'"]+)['"]\s*\)/g;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Collect all package names that are imported or required in the given file content.
|
|
39
|
+
*/
|
|
40
|
+
function extractFromContent(content: string): Set<string> {
|
|
41
|
+
const packages = new Set<string>();
|
|
42
|
+
let m: RegExpExecArray | null;
|
|
43
|
+
RE_IMPORT.lastIndex = 0;
|
|
44
|
+
while ((m = RE_IMPORT.exec(content)) !== null) {
|
|
45
|
+
const specifier = (m[1] ?? m[2] ?? m[3]) ?? "";
|
|
46
|
+
const pkg = specifierToPackageName(specifier);
|
|
47
|
+
if (pkg) packages.add(pkg);
|
|
48
|
+
}
|
|
49
|
+
return packages;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Collect all package names imported by any of the given files (by their content).
|
|
54
|
+
*/
|
|
55
|
+
export function extractImportedPackages(files: { content: string }[]): Set<string> {
|
|
56
|
+
const all = new Set<string>();
|
|
57
|
+
for (const { content } of files) {
|
|
58
|
+
for (const pkg of extractFromContent(content)) {
|
|
59
|
+
all.add(pkg);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return all;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** True if the import specifier refers to a project file (relative or path alias), not npm. */
|
|
66
|
+
export function isPathSpecifier(specifier: string): boolean {
|
|
67
|
+
const s = specifier.trim();
|
|
68
|
+
if (!s || s.startsWith("node:")) return false;
|
|
69
|
+
if (s.startsWith(".") || s.startsWith("/")) return true;
|
|
70
|
+
if (s.startsWith("@/") || s.startsWith("~/") || s.startsWith("#")) return true;
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Extract all import/require specifiers that are project paths (relative or alias)
|
|
76
|
+
* from file content. Used to pull in imported files into the registry.
|
|
77
|
+
*/
|
|
78
|
+
export function extractPathSpecifiers(content: string): string[] {
|
|
79
|
+
const out: string[] = [];
|
|
80
|
+
let m: RegExpExecArray | null;
|
|
81
|
+
RE_IMPORT.lastIndex = 0;
|
|
82
|
+
while ((m = RE_IMPORT.exec(content)) !== null) {
|
|
83
|
+
const specifier = (m[1] ?? m[2] ?? m[3]) ?? "";
|
|
84
|
+
if (isPathSpecifier(specifier)) out.push(specifier);
|
|
85
|
+
}
|
|
86
|
+
return out;
|
|
87
|
+
}
|
|
@@ -2,7 +2,6 @@ import fs from "node:fs";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { execSync } from "node:child_process";
|
|
4
4
|
import ignore from "ignore";
|
|
5
|
-
import { start } from "node:repl";
|
|
6
5
|
|
|
7
6
|
const INITIAL_DIR = "_initial";
|
|
8
7
|
|
|
@@ -32,62 +31,67 @@ const EXCLUDE_FILES = [
|
|
|
32
31
|
"favicon.ico",
|
|
33
32
|
];
|
|
34
33
|
|
|
35
|
-
function cloneInitialCommit() {
|
|
34
|
+
function cloneInitialCommit(): void {
|
|
36
35
|
deleteInitialDir();
|
|
37
36
|
|
|
38
37
|
try {
|
|
39
|
-
// Get the initial commit hash
|
|
40
38
|
const initialCommit = execSync("git rev-list --max-parents=0 HEAD")
|
|
41
39
|
.toString()
|
|
42
40
|
.trim();
|
|
43
41
|
|
|
44
|
-
// Clone the initial commit quietly
|
|
45
42
|
execSync(`git worktree add -f ${INITIAL_DIR} ${initialCommit}`, {
|
|
46
43
|
stdio: "ignore",
|
|
47
44
|
});
|
|
48
|
-
} catch (error) {
|
|
45
|
+
} catch (error: any) {
|
|
49
46
|
console.error("Error cloning initial commit:", error.message);
|
|
50
47
|
process.exit(1);
|
|
51
48
|
}
|
|
52
49
|
}
|
|
53
50
|
|
|
54
|
-
function deleteInitialDir() {
|
|
51
|
+
function deleteInitialDir(): void {
|
|
55
52
|
if (fs.existsSync(INITIAL_DIR)) {
|
|
56
|
-
fs.rmSync(INITIAL_DIR, { recursive: true });
|
|
53
|
+
fs.rmSync(INITIAL_DIR, { recursive: true, force: true });
|
|
57
54
|
|
|
58
55
|
try {
|
|
59
56
|
execSync("git worktree prune", { stdio: "ignore" });
|
|
60
|
-
} catch (error) {
|
|
57
|
+
} catch (error: any) {
|
|
61
58
|
console.error("Error pruning git worktree:", error.message);
|
|
62
59
|
}
|
|
63
60
|
}
|
|
64
61
|
}
|
|
65
62
|
|
|
66
|
-
function checkIfFileIsChanged(relativeFilePath) {
|
|
63
|
+
function checkIfFileIsChanged(relativeFilePath: string): boolean {
|
|
67
64
|
const initialFilePath = path.join(INITIAL_DIR, relativeFilePath);
|
|
68
65
|
const fullPath = path.join(process.cwd(), relativeFilePath);
|
|
69
66
|
if (!fs.existsSync(initialFilePath)) {
|
|
70
|
-
return true;
|
|
67
|
+
return true;
|
|
71
68
|
}
|
|
72
69
|
const currentContent = fs.readFileSync(fullPath, "utf-8");
|
|
73
70
|
const initialContent = fs.readFileSync(initialFilePath, "utf-8");
|
|
74
71
|
return currentContent !== initialContent;
|
|
75
72
|
}
|
|
76
73
|
|
|
77
|
-
export
|
|
78
|
-
|
|
74
|
+
export interface ScannedFile {
|
|
75
|
+
path: string;
|
|
76
|
+
content: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function scanForFiles(
|
|
80
|
+
startDir: string,
|
|
81
|
+
checkFile = false,
|
|
82
|
+
): ScannedFile[] {
|
|
83
|
+
const foundFiles: ScannedFile[] = [];
|
|
79
84
|
|
|
80
|
-
let ignorer = () => false;
|
|
81
|
-
|
|
85
|
+
let ignorer: (relativeFilePath: string) => boolean = () => false;
|
|
86
|
+
const gitignorePath = path.join(startDir, ".gitignore");
|
|
87
|
+
if (fs.existsSync(gitignorePath)) {
|
|
82
88
|
const gitIgnore = ignore().add(
|
|
83
|
-
fs.readFileSync(
|
|
89
|
+
fs.readFileSync(gitignorePath).toString(),
|
|
84
90
|
);
|
|
85
|
-
ignorer = (relativeFilePath) =>
|
|
86
|
-
return gitIgnore.ignores(relativeFilePath);
|
|
87
|
-
};
|
|
91
|
+
ignorer = (relativeFilePath: string) => gitIgnore.ignores(relativeFilePath);
|
|
88
92
|
}
|
|
89
93
|
|
|
90
|
-
function scanDirectory(dir, relativePath = "") {
|
|
94
|
+
function scanDirectory(dir: string, relativePath = ""): void {
|
|
91
95
|
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
92
96
|
|
|
93
97
|
for (const entry of entries) {
|
|
@@ -102,7 +106,10 @@ export function scanForFiles(startDir, checkFile = false) {
|
|
|
102
106
|
!checkFile ||
|
|
103
107
|
(checkFile && checkIfFileIsChanged(relativeFilePath))
|
|
104
108
|
) {
|
|
105
|
-
if (
|
|
109
|
+
if (
|
|
110
|
+
!EXCLUDE_FILES.includes(entry.name) &&
|
|
111
|
+
!ignorer(relativeFilePath)
|
|
112
|
+
) {
|
|
106
113
|
foundFiles.push({
|
|
107
114
|
path: relativeFilePath,
|
|
108
115
|
content: fs.readFileSync(fullPath, "utf-8"),
|
|
@@ -117,16 +124,26 @@ export function scanForFiles(startDir, checkFile = false) {
|
|
|
117
124
|
return foundFiles.sort((a, b) => a.path.localeCompare(b.path));
|
|
118
125
|
}
|
|
119
126
|
|
|
120
|
-
export function scanForAlteredFiles(
|
|
127
|
+
export function scanForAlteredFiles(
|
|
128
|
+
specificFilesToReturn: string[] = [],
|
|
129
|
+
): {
|
|
130
|
+
alteredFiles: ScannedFile[];
|
|
131
|
+
specificFiles: Record<string, string>;
|
|
132
|
+
} {
|
|
121
133
|
cloneInitialCommit();
|
|
122
134
|
|
|
123
135
|
const alteredFiles = scanForFiles(process.cwd(), true);
|
|
124
136
|
|
|
125
|
-
const specificFiles = specificFilesToReturn.reduce
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
137
|
+
const specificFiles = specificFilesToReturn.reduce<Record<string, string>>(
|
|
138
|
+
(out, file) => {
|
|
139
|
+
const fullPath = path.join(process.cwd(), INITIAL_DIR, file);
|
|
140
|
+
out[file] = fs.existsSync(fullPath)
|
|
141
|
+
? fs.readFileSync(fullPath, "utf-8")
|
|
142
|
+
: "";
|
|
143
|
+
return out;
|
|
144
|
+
},
|
|
145
|
+
{},
|
|
146
|
+
);
|
|
130
147
|
|
|
131
148
|
deleteInitialDir();
|
|
132
149
|
|
|
@@ -136,6 +153,7 @@ export function scanForAlteredFiles(specificFilesToReturn = []) {
|
|
|
136
153
|
};
|
|
137
154
|
}
|
|
138
155
|
|
|
139
|
-
export function hasSrcDir(dir) {
|
|
156
|
+
export function hasSrcDir(dir: string): boolean {
|
|
140
157
|
return fs.existsSync(path.join(dir, "src"));
|
|
141
158
|
}
|
|
159
|
+
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { execSync } from "node:child_process";
|
|
6
|
+
import { program } from "commander";
|
|
7
|
+
|
|
8
|
+
import {
|
|
9
|
+
scanForAlteredFiles,
|
|
10
|
+
scanForFiles,
|
|
11
|
+
hasSrcDir,
|
|
12
|
+
type ScannedFile,
|
|
13
|
+
} from "./git.js";
|
|
14
|
+
import { readComponentsManifest } from "./components.js";
|
|
15
|
+
import { createDiff } from "./create-diff.js";
|
|
16
|
+
import { expandIncludedFiles } from "./resolve-imports.js";
|
|
17
|
+
|
|
18
|
+
function runCommand(command: string): void {
|
|
19
|
+
try {
|
|
20
|
+
execSync(command, { stdio: "inherit" });
|
|
21
|
+
} catch (error) {
|
|
22
|
+
console.error(`Failed to execute command: ${command}`);
|
|
23
|
+
process.exit(1);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function ensureGitignore(): void {
|
|
28
|
+
const gitignorePath = path.join(process.cwd(), ".gitignore");
|
|
29
|
+
if (!fs.existsSync(gitignorePath)) {
|
|
30
|
+
const content = `
|
|
31
|
+
/node_modules
|
|
32
|
+
/.pnp
|
|
33
|
+
.pnp.*
|
|
34
|
+
.yarn/*
|
|
35
|
+
!.yarn/patches
|
|
36
|
+
!.yarn/plugins
|
|
37
|
+
!.yarn/releases
|
|
38
|
+
!.yarn/versions
|
|
39
|
+
|
|
40
|
+
# testing
|
|
41
|
+
/coverage
|
|
42
|
+
|
|
43
|
+
# next.js
|
|
44
|
+
/.next/
|
|
45
|
+
/out/
|
|
46
|
+
|
|
47
|
+
# production
|
|
48
|
+
/build
|
|
49
|
+
|
|
50
|
+
# misc
|
|
51
|
+
.DS_Store
|
|
52
|
+
*.pem
|
|
53
|
+
|
|
54
|
+
# debug
|
|
55
|
+
npm-debug.log*
|
|
56
|
+
yarn-debug.log*
|
|
57
|
+
yarn-error.log*
|
|
58
|
+
|
|
59
|
+
# env files (can opt-in for committing if needed)
|
|
60
|
+
.env*
|
|
61
|
+
|
|
62
|
+
# vercel
|
|
63
|
+
.vercel
|
|
64
|
+
|
|
65
|
+
# typescript
|
|
66
|
+
*.tsbuildinfo
|
|
67
|
+
next-env.d.ts
|
|
68
|
+
`;
|
|
69
|
+
fs.writeFileSync(gitignorePath, content, "utf8");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function main(): void {
|
|
74
|
+
program
|
|
75
|
+
.option("-n, --name <name>")
|
|
76
|
+
.option("--init")
|
|
77
|
+
.option("-f, --folder <folder>", "folder to include (default: current directory)")
|
|
78
|
+
.option("--git", "use git mode: only files changed since initial commit");
|
|
79
|
+
program.parse();
|
|
80
|
+
|
|
81
|
+
const options = program.opts<{
|
|
82
|
+
name?: string;
|
|
83
|
+
init?: boolean;
|
|
84
|
+
folder?: string;
|
|
85
|
+
git?: boolean;
|
|
86
|
+
}>();
|
|
87
|
+
|
|
88
|
+
if (options.init) {
|
|
89
|
+
// Initialize a clean git repository for the current component
|
|
90
|
+
if (process.platform === "win32") {
|
|
91
|
+
runCommand(
|
|
92
|
+
'rmdir /s /q .git && git init && git add . && git commit -m "Initial commit"',
|
|
93
|
+
);
|
|
94
|
+
} else {
|
|
95
|
+
runCommand(
|
|
96
|
+
'rm -fr .git && git init && git add . && git commit -m "Initial commit"',
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
ensureGitignore();
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const useFolderMode = !options.git;
|
|
104
|
+
const folderOpt = options.folder ?? (useFolderMode ? "." : undefined);
|
|
105
|
+
|
|
106
|
+
const baseName = folderOpt != null
|
|
107
|
+
? path.basename(path.resolve(process.cwd(), folderOpt))
|
|
108
|
+
: path.basename(process.cwd());
|
|
109
|
+
const name = options.name || baseName;
|
|
110
|
+
|
|
111
|
+
let alteredFiles: ScannedFile[];
|
|
112
|
+
let specificFiles: Record<string, string>;
|
|
113
|
+
|
|
114
|
+
if (useFolderMode && folderOpt != null) {
|
|
115
|
+
const folderPath = path.resolve(process.cwd(), folderOpt);
|
|
116
|
+
if (!fs.existsSync(folderPath) || !fs.statSync(folderPath).isDirectory()) {
|
|
117
|
+
console.error(
|
|
118
|
+
`Folder path is not a directory: ${folderOpt}`,
|
|
119
|
+
);
|
|
120
|
+
process.exit(1);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const folderPrefix = path
|
|
124
|
+
.relative(process.cwd(), folderPath)
|
|
125
|
+
.replace(/\\/g, "/")
|
|
126
|
+
.replace(/^\.\//, "");
|
|
127
|
+
|
|
128
|
+
const allFiles = scanForFiles(process.cwd());
|
|
129
|
+
alteredFiles = allFiles.filter(({ path: filePath }) => {
|
|
130
|
+
const normalized = filePath.replace(/\\/g, "/");
|
|
131
|
+
return (
|
|
132
|
+
normalized === folderPrefix ||
|
|
133
|
+
normalized.startsWith(`${folderPrefix}/`)
|
|
134
|
+
);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
specificFiles = {
|
|
138
|
+
"./package.json": "{}",
|
|
139
|
+
};
|
|
140
|
+
} else {
|
|
141
|
+
const result = scanForAlteredFiles(["./package.json"]);
|
|
142
|
+
alteredFiles = result.alteredFiles;
|
|
143
|
+
specificFiles = result.specificFiles;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const currentFiles = scanForFiles(process.cwd());
|
|
147
|
+
|
|
148
|
+
const currentPackageJson = fs.readFileSync("./package.json", "utf-8");
|
|
149
|
+
|
|
150
|
+
const config = readComponentsManifest(process.cwd());
|
|
151
|
+
(config as any).isSrcDir = hasSrcDir(process.cwd());
|
|
152
|
+
|
|
153
|
+
// Recursively add any project file imported by the included files so the
|
|
154
|
+
// registry item is self-contained (no "component not found").
|
|
155
|
+
alteredFiles = expandIncludedFiles(alteredFiles, currentFiles, config as any);
|
|
156
|
+
|
|
157
|
+
const output = createDiff({
|
|
158
|
+
name,
|
|
159
|
+
config,
|
|
160
|
+
alteredFiles,
|
|
161
|
+
currentFiles,
|
|
162
|
+
specificFiles,
|
|
163
|
+
currentPackageJson,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// Emit a complete registry:block JSON object suitable for local file support
|
|
167
|
+
// (e.g. `npx shadcn add ./block.json`)
|
|
168
|
+
// eslint-disable-next-line no-console
|
|
169
|
+
console.log(JSON.stringify(output, null, 2));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
main();
|
|
173
|
+
|
|
@@ -1,24 +1,60 @@
|
|
|
1
1
|
import path from "node:path";
|
|
2
|
+
import type { ComponentsConfig } from "./components.js";
|
|
2
3
|
|
|
3
|
-
function fixAlias(alias) {
|
|
4
|
+
function fixAlias(alias: string): string {
|
|
4
5
|
return alias.replace("@", ".");
|
|
5
6
|
}
|
|
6
7
|
|
|
7
|
-
export
|
|
8
|
+
export type RegistryFileType =
|
|
9
|
+
| "registry:example"
|
|
10
|
+
| "registry:ui"
|
|
11
|
+
| "registry:block"
|
|
12
|
+
| "registry:hook"
|
|
13
|
+
| "registry:lib"
|
|
14
|
+
| "registry:page"
|
|
15
|
+
| "registry:theme"
|
|
16
|
+
| "registry:style"
|
|
17
|
+
| "registry:file"
|
|
18
|
+
| "registry:component";
|
|
19
|
+
|
|
20
|
+
export interface ParsedFile {
|
|
21
|
+
path: string;
|
|
22
|
+
content: string;
|
|
23
|
+
type: RegistryFileType;
|
|
24
|
+
target?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseFilePath(
|
|
28
|
+
wasInSrcDir: boolean,
|
|
29
|
+
config: ComponentsConfig,
|
|
30
|
+
filePath: string,
|
|
31
|
+
content: string,
|
|
32
|
+
): ParsedFile {
|
|
8
33
|
const normalizedPath = filePath.replace(/^\.\//, "");
|
|
9
34
|
const extension = path.extname(normalizedPath);
|
|
10
35
|
const baseName = path.basename(normalizedPath);
|
|
11
36
|
|
|
12
|
-
const styleExtensions = new Set([
|
|
13
|
-
|
|
37
|
+
const styleExtensions = new Set([
|
|
38
|
+
".css",
|
|
39
|
+
".scss",
|
|
40
|
+
".sass",
|
|
41
|
+
".less",
|
|
42
|
+
".pcss",
|
|
43
|
+
]);
|
|
44
|
+
const fileExtensions = new Set([
|
|
45
|
+
".json",
|
|
46
|
+
".yaml",
|
|
47
|
+
".yml",
|
|
48
|
+
".md",
|
|
49
|
+
".mdx",
|
|
50
|
+
".txt",
|
|
51
|
+
]);
|
|
14
52
|
const themePattern = /(\/|^)(theme|.*-theme)(\.[a-z0-9]+)?$/i;
|
|
15
53
|
|
|
16
54
|
const sanitizedTargetPath = normalizedPath.replace(/^src\//, "");
|
|
17
|
-
const defaultTarget = wasInSrcDir
|
|
18
|
-
? filePath
|
|
19
|
-
: `~/${normalizedPath}`;
|
|
55
|
+
const defaultTarget = wasInSrcDir ? filePath : `~/${normalizedPath}`;
|
|
20
56
|
|
|
21
|
-
const out = {
|
|
57
|
+
const out: ParsedFile = {
|
|
22
58
|
path: filePath,
|
|
23
59
|
content,
|
|
24
60
|
type: "registry:example",
|
|
@@ -52,13 +88,10 @@ export function parseFilePath(wasInSrcDir, config, filePath, content) {
|
|
|
52
88
|
out.type = "registry:component";
|
|
53
89
|
out.target = undefined;
|
|
54
90
|
} else {
|
|
55
|
-
out.type = "registry:
|
|
56
|
-
out.target =
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (out.type === "registry:example") {
|
|
60
|
-
out.path = filePath;
|
|
91
|
+
out.type = "registry:file";
|
|
92
|
+
out.target = `./${sanitizedTargetPath}`;
|
|
61
93
|
}
|
|
62
94
|
|
|
63
95
|
return out;
|
|
64
96
|
}
|
|
97
|
+
|
package/src/release.ts
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
type VersionBump = "patch" | "minor" | "major";
|
|
4
|
+
|
|
5
|
+
function run(command: string, args: string[] = []) {
|
|
6
|
+
const full = [command, ...args].join(" ");
|
|
7
|
+
console.log(`\n$ ${full}`);
|
|
8
|
+
|
|
9
|
+
const result = spawnSync(command, args, {
|
|
10
|
+
stdio: "inherit",
|
|
11
|
+
shell: process.platform === "win32",
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
if (result.status !== 0) {
|
|
15
|
+
console.error(`\nCommand failed: ${full}`);
|
|
16
|
+
process.exit(result.status === null ? 1 : result.status);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getVersionBumpFromArgs(): VersionBump {
|
|
21
|
+
const type = (process.argv[2] as VersionBump | undefined) ?? "patch";
|
|
22
|
+
if (!["patch", "minor", "major"].includes(type)) {
|
|
23
|
+
console.error(
|
|
24
|
+
`Invalid version bump "${type}". Use one of: patch, minor, major.`
|
|
25
|
+
);
|
|
26
|
+
process.exit(1);
|
|
27
|
+
}
|
|
28
|
+
return type;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function ensureCleanGit() {
|
|
32
|
+
const result = spawnSync("git", ["status", "--porcelain"], {
|
|
33
|
+
encoding: "utf8",
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
if (result.status !== 0) {
|
|
37
|
+
console.error("Failed to check git status.");
|
|
38
|
+
process.exit(1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
if ((result.stdout ?? "").trim().length > 0) {
|
|
42
|
+
console.error(
|
|
43
|
+
"Git working tree is not clean. Commit or stash your changes before releasing."
|
|
44
|
+
);
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function main() {
|
|
50
|
+
const bump = getVersionBumpFromArgs();
|
|
51
|
+
|
|
52
|
+
console.log("Ensuring clean git working tree...");
|
|
53
|
+
ensureCleanGit();
|
|
54
|
+
|
|
55
|
+
console.log("\nBuilding project...");
|
|
56
|
+
run("npm", ["run", "build"]);
|
|
57
|
+
|
|
58
|
+
console.log(`\nBumping version (${bump})...`);
|
|
59
|
+
run("npm", ["version", bump]);
|
|
60
|
+
|
|
61
|
+
console.log("\nPublishing to npm...");
|
|
62
|
+
run("npm", ["publish"]);
|
|
63
|
+
|
|
64
|
+
console.log("\nRelease complete.");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
main().catch((err) => {
|
|
68
|
+
console.error(err);
|
|
69
|
+
process.exit(1);
|
|
70
|
+
});
|
|
71
|
+
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import type { ComponentsConfig } from "./components.js";
|
|
3
|
+
import { extractPathSpecifiers } from "./extract-imports.js";
|
|
4
|
+
import type { ScannedFile } from "./git.js";
|
|
5
|
+
|
|
6
|
+
const EXTENSIONS = [".tsx", ".ts", ".jsx", ".js", ".mts", ".mjs", ".cts", ".cjs"];
|
|
7
|
+
const INDEX_NAMES = ["index.tsx", "index.ts", "index.jsx", "index.js"];
|
|
8
|
+
|
|
9
|
+
/** Build [aliasPrefix, pathPrefix] pairs, longest first. pathPrefix is project-relative (e.g. src/lib when isSrcDir). */
|
|
10
|
+
function getAliasEntries(config: ComponentsConfig & { isSrcDir?: boolean }): [string, string][] {
|
|
11
|
+
const entries: [string, string][] = [];
|
|
12
|
+
const prefix = config.isSrcDir ? "src/" : "";
|
|
13
|
+
const keys: (keyof ComponentsConfig)[] = ["components", "utils", "ui", "lib", "hooks"];
|
|
14
|
+
for (const k of keys) {
|
|
15
|
+
const v = config[k];
|
|
16
|
+
if (typeof v === "string" && v.startsWith("@/")) {
|
|
17
|
+
const pathPart = v.replace(/^@\//, "").replace(/\/$/, "");
|
|
18
|
+
entries.push([v.replace(/\/$/, ""), prefix + pathPart]);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
entries.push(["@", config.isSrcDir ? "src" : ""]);
|
|
22
|
+
entries.sort((a, b) => b[0].length - a[0].length);
|
|
23
|
+
return entries;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Resolve a path-like import specifier to a project-relative file path.
|
|
28
|
+
* Tries common extensions and /index.*. Returns the first path that exists in projectPaths.
|
|
29
|
+
*/
|
|
30
|
+
function resolveSpecifier(
|
|
31
|
+
specifier: string,
|
|
32
|
+
fromFilePath: string,
|
|
33
|
+
aliasEntries: [string, string][],
|
|
34
|
+
projectPaths: Set<string>,
|
|
35
|
+
rootFallback: string,
|
|
36
|
+
): string | null {
|
|
37
|
+
const normalizedFrom = fromFilePath.replace(/\\/g, "/");
|
|
38
|
+
const fromDir = path.dirname(normalizedFrom).replace(/\\/g, "/");
|
|
39
|
+
|
|
40
|
+
let candidate: string;
|
|
41
|
+
|
|
42
|
+
if (specifier.startsWith(".") || specifier.startsWith("/")) {
|
|
43
|
+
candidate = path.normalize(path.join(fromDir, specifier)).replace(/\\/g, "/");
|
|
44
|
+
} else {
|
|
45
|
+
// Alias: @/components/Button -> components/Button
|
|
46
|
+
let matched = false;
|
|
47
|
+
for (const [aliasPrefix, pathPrefix] of aliasEntries) {
|
|
48
|
+
const prefix = aliasPrefix === "@" ? "@/" : aliasPrefix.endsWith("/") ? aliasPrefix : aliasPrefix + "/";
|
|
49
|
+
if (specifier === aliasPrefix || specifier.startsWith(prefix)) {
|
|
50
|
+
const suffix = specifier.slice(prefix.length).replace(/^\//, "");
|
|
51
|
+
candidate = path.normalize(path.join(pathPrefix, suffix)).replace(/\\/g, "/");
|
|
52
|
+
matched = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (!matched) {
|
|
57
|
+
const suffix = specifier.replace(/^@\//, "").replace(/^~\//, "");
|
|
58
|
+
candidate = path.normalize(path.join(rootFallback, suffix)).replace(/\\/g, "/");
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (!candidate) return null;
|
|
63
|
+
if (projectPaths.has(candidate)) return candidate;
|
|
64
|
+
|
|
65
|
+
for (const ext of EXTENSIONS) {
|
|
66
|
+
const p = candidate.endsWith(ext) ? candidate : candidate + ext;
|
|
67
|
+
if (projectPaths.has(p)) return p;
|
|
68
|
+
}
|
|
69
|
+
for (const name of INDEX_NAMES) {
|
|
70
|
+
const p = candidate.endsWith("/") ? candidate + name : candidate + "/" + name;
|
|
71
|
+
if (projectPaths.has(p)) return p;
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Expand the list of included files by recursively adding any project file
|
|
78
|
+
* that is imported (via relative or alias path) by an already-included file.
|
|
79
|
+
* Uses TypeScript/JS import syntax only.
|
|
80
|
+
*/
|
|
81
|
+
export function expandIncludedFiles(
|
|
82
|
+
includedFiles: ScannedFile[],
|
|
83
|
+
allProjectFiles: ScannedFile[],
|
|
84
|
+
config: ComponentsConfig & { isSrcDir?: boolean },
|
|
85
|
+
): ScannedFile[] {
|
|
86
|
+
const projectPathToFile = new Map<string, ScannedFile>();
|
|
87
|
+
for (const f of allProjectFiles) {
|
|
88
|
+
const key = f.path.replace(/\\/g, "/");
|
|
89
|
+
projectPathToFile.set(key, f);
|
|
90
|
+
}
|
|
91
|
+
const projectPaths = new Set(projectPathToFile.keys());
|
|
92
|
+
const aliasEntries = getAliasEntries(config);
|
|
93
|
+
const rootFallback = config.isSrcDir ? "src" : "";
|
|
94
|
+
|
|
95
|
+
const includedPaths = new Set<string>();
|
|
96
|
+
for (const f of includedFiles) {
|
|
97
|
+
includedPaths.add(f.path.replace(/\\/g, "/"));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
let added = true;
|
|
101
|
+
while (added) {
|
|
102
|
+
added = false;
|
|
103
|
+
for (const file of [...includedFiles]) {
|
|
104
|
+
const content = file.content;
|
|
105
|
+
const fromPath = file.path.replace(/\\/g, "/");
|
|
106
|
+
for (const spec of extractPathSpecifiers(content)) {
|
|
107
|
+
const resolved = resolveSpecifier(spec, fromPath, aliasEntries, projectPaths, rootFallback);
|
|
108
|
+
if (resolved && !includedPaths.has(resolved)) {
|
|
109
|
+
const scanned = projectPathToFile.get(resolved);
|
|
110
|
+
if (scanned) {
|
|
111
|
+
includedFiles.push(scanned);
|
|
112
|
+
includedPaths.add(resolved);
|
|
113
|
+
added = true;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return includedFiles;
|
|
121
|
+
}
|