@nooeh/cli 0.1.0
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 +201 -0
- package/dist/add.d.ts +7 -0
- package/dist/add.js +105 -0
- package/dist/arguments.d.ts +15 -0
- package/dist/arguments.js +21 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +4 -0
- package/dist/config.d.ts +16 -0
- package/dist/config.js +114 -0
- package/dist/dependencies.d.ts +1 -0
- package/dist/dependencies.js +30 -0
- package/dist/docs.d.ts +2 -0
- package/dist/docs.js +23 -0
- package/dist/doctor.d.ts +1 -0
- package/dist/doctor.js +165 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +48 -0
- package/dist/init.d.ts +2 -0
- package/dist/init.js +142 -0
- package/dist/new-component.d.ts +1 -0
- package/dist/new-component.js +42 -0
- package/dist/registry.d.ts +6 -0
- package/dist/registry.js +81 -0
- package/package.json +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/dist/add.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type AddOptions = {
|
|
2
|
+
defaults?: boolean;
|
|
3
|
+
"dry-run"?: boolean;
|
|
4
|
+
skipDependencyInstall?: boolean;
|
|
5
|
+
"skip-dependencies"?: boolean;
|
|
6
|
+
} & Record<string, boolean | string | undefined>;
|
|
7
|
+
export declare function add(projectDirectory: string, componentName: string, options?: AddOptions): Promise<void>;
|
package/dist/add.js
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
|
+
import { createInterface } from "node:readline/promises";
|
|
4
|
+
import { defaultConfig, hasConfig, readConfig, resolveAliasPath, resolveTokensPath, } from "./config.js";
|
|
5
|
+
import { installDependencies } from "./dependencies.js";
|
|
6
|
+
import { init } from "./init.js";
|
|
7
|
+
import { resolveComponent } from "./registry.js";
|
|
8
|
+
function isNotFoundError(error) {
|
|
9
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
10
|
+
}
|
|
11
|
+
async function askYesNo(question, defaultValue) {
|
|
12
|
+
if (!process.stdin.isTTY || !process.stdout.isTTY)
|
|
13
|
+
return defaultValue;
|
|
14
|
+
const readline = createInterface({ input: process.stdin, output: process.stdout });
|
|
15
|
+
const defaultLabel = defaultValue ? "Y/n" : "y/N";
|
|
16
|
+
try {
|
|
17
|
+
const answer = (await readline.question(`${question} (${defaultLabel}) `)).trim().toLowerCase();
|
|
18
|
+
if (!answer)
|
|
19
|
+
return defaultValue;
|
|
20
|
+
return answer === "y" || answer === "yes";
|
|
21
|
+
}
|
|
22
|
+
finally {
|
|
23
|
+
readline.close();
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
async function writeSource(source, targetPath, confirmOverwrite) {
|
|
27
|
+
try {
|
|
28
|
+
const currentSource = await readFile(targetPath, "utf8");
|
|
29
|
+
if (currentSource === source)
|
|
30
|
+
return "unchanged";
|
|
31
|
+
if (!(await confirmOverwrite()))
|
|
32
|
+
throw new Error("Component installation canceled.");
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
if (!isNotFoundError(error))
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
await mkdir(dirname(targetPath), { recursive: true });
|
|
39
|
+
await writeFile(targetPath, source, "utf8");
|
|
40
|
+
return "written";
|
|
41
|
+
}
|
|
42
|
+
function resolveTargetPath(uiDirectory, filePath) {
|
|
43
|
+
if (isAbsolute(filePath))
|
|
44
|
+
throw new Error(`Registry file path must be relative: ${filePath}`);
|
|
45
|
+
const targetPath = resolve(uiDirectory, filePath);
|
|
46
|
+
const relativePath = relative(uiDirectory, targetPath);
|
|
47
|
+
if (relativePath === ".." || relativePath.startsWith(`..${"/"}`) || isAbsolute(relativePath)) {
|
|
48
|
+
throw new Error(`Registry file path must stay inside the UI directory: ${filePath}`);
|
|
49
|
+
}
|
|
50
|
+
return targetPath;
|
|
51
|
+
}
|
|
52
|
+
function replaceTokenImport(source, targetPath, tokenDirectory) {
|
|
53
|
+
const tokenModulePath = join(tokenDirectory, "tokens.stylex.ts");
|
|
54
|
+
const relativePath = relative(dirname(targetPath), tokenModulePath)
|
|
55
|
+
.replace(/\.ts$/, "")
|
|
56
|
+
.split(sep)
|
|
57
|
+
.join("/");
|
|
58
|
+
const tokenModule = relativePath.startsWith(".") ? relativePath : `./${relativePath}`;
|
|
59
|
+
return source.replaceAll("@nooeh/tokens/tokens.stylex", tokenModule);
|
|
60
|
+
}
|
|
61
|
+
export async function add(projectDirectory, componentName, options = {}) {
|
|
62
|
+
if (!componentName)
|
|
63
|
+
throw new Error("Enter a component name to add.");
|
|
64
|
+
const shouldInitialize = !(await hasConfig(projectDirectory));
|
|
65
|
+
if (shouldInitialize && !options["dry-run"]) {
|
|
66
|
+
await init(projectDirectory, {
|
|
67
|
+
...options,
|
|
68
|
+
"skip-dependencies": options.skipDependencyInstall || options["skip-dependencies"],
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const config = options["dry-run"] && shouldInitialize ? defaultConfig : await readConfig(projectDirectory);
|
|
72
|
+
const resolved = await resolveComponent(componentName);
|
|
73
|
+
const uiDirectory = await resolveAliasPath(projectDirectory, config.aliases.ui);
|
|
74
|
+
const tokenDirectory = resolveTokensPath(projectDirectory, config.tokens);
|
|
75
|
+
let isOverwriteConfirmed = false;
|
|
76
|
+
async function confirmOverwrite() {
|
|
77
|
+
if (isOverwriteConfirmed)
|
|
78
|
+
return true;
|
|
79
|
+
isOverwriteConfirmed = await askYesNo("Files with the same names already exist. Overwrite all?", false);
|
|
80
|
+
return isOverwriteConfirmed;
|
|
81
|
+
}
|
|
82
|
+
for (const file of resolved.files) {
|
|
83
|
+
const targetPath = resolveTargetPath(uiDirectory, file.path);
|
|
84
|
+
if (options["dry-run"]) {
|
|
85
|
+
console.log(`Will add: ${targetPath}`);
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
await writeSource(replaceTokenImport(file.content, targetPath, tokenDirectory), targetPath, confirmOverwrite);
|
|
89
|
+
}
|
|
90
|
+
const shouldInstallDependencies = resolved.externalDependencies.length > 0 &&
|
|
91
|
+
!options.skipDependencyInstall &&
|
|
92
|
+
!options["skip-dependencies"] &&
|
|
93
|
+
!options["dry-run"] &&
|
|
94
|
+
(await askYesNo(`Install external dependencies (${resolved.externalDependencies.join(", ")})?`, true));
|
|
95
|
+
if (shouldInstallDependencies) {
|
|
96
|
+
await installDependencies(projectDirectory, resolved.externalDependencies);
|
|
97
|
+
}
|
|
98
|
+
const primaryExport = resolved.primaryExport;
|
|
99
|
+
if (options["dry-run"]) {
|
|
100
|
+
console.log(`Will add ${componentName}.`);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
console.log(`Added ${componentName}.`);
|
|
104
|
+
console.log(`import { ${primaryExport} } from "${config.aliases.ui}/${componentName}"`);
|
|
105
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type CliOptions = Record<string, boolean | string | undefined> & {
|
|
2
|
+
cwd?: string;
|
|
3
|
+
defaults?: boolean;
|
|
4
|
+
framework?: string;
|
|
5
|
+
"dry-run"?: boolean;
|
|
6
|
+
force?: boolean;
|
|
7
|
+
"skip-dependencies"?: boolean;
|
|
8
|
+
tokens?: string;
|
|
9
|
+
"ui-alias"?: string;
|
|
10
|
+
};
|
|
11
|
+
export declare function parseArguments(arguments_: readonly string[]): {
|
|
12
|
+
command: string;
|
|
13
|
+
options: CliOptions;
|
|
14
|
+
positionals: string[];
|
|
15
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export function parseArguments(arguments_) {
|
|
2
|
+
const [command, ...rest] = arguments_;
|
|
3
|
+
const options = {};
|
|
4
|
+
const positionals = [];
|
|
5
|
+
for (let index = 0; index < rest.length; index += 1) {
|
|
6
|
+
const argument = rest[index];
|
|
7
|
+
if (!argument.startsWith("--")) {
|
|
8
|
+
positionals.push(argument);
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
const name = argument.slice(2);
|
|
12
|
+
const nextArgument = rest[index + 1];
|
|
13
|
+
if (!nextArgument || nextArgument.startsWith("--")) {
|
|
14
|
+
options[name] = true;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
options[name] = nextArgument;
|
|
18
|
+
index += 1;
|
|
19
|
+
}
|
|
20
|
+
return { command, options, positionals };
|
|
21
|
+
}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const configFileName = "nooeh.json";
|
|
2
|
+
export declare const configVersion = 2;
|
|
3
|
+
export type NooehConfig = {
|
|
4
|
+
aliases: {
|
|
5
|
+
ui: string;
|
|
6
|
+
};
|
|
7
|
+
tokens: string;
|
|
8
|
+
version?: number;
|
|
9
|
+
};
|
|
10
|
+
export declare const defaultConfig: NooehConfig;
|
|
11
|
+
export declare function hasConfig(projectDirectory: string): Promise<boolean>;
|
|
12
|
+
export declare function validateConfig(config: unknown): NooehConfig;
|
|
13
|
+
export declare function readConfig(projectDirectory: string): Promise<NooehConfig>;
|
|
14
|
+
export declare function writeConfig(projectDirectory: string, config: NooehConfig): Promise<string>;
|
|
15
|
+
export declare function resolveTokensPath(projectDirectory: string, tokens: string): string;
|
|
16
|
+
export declare function resolveAliasPath(projectDirectory: string, alias: string): Promise<string>;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { access, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { isAbsolute, relative, resolve } from "node:path";
|
|
3
|
+
export const configFileName = "nooeh.json";
|
|
4
|
+
export const configVersion = 2;
|
|
5
|
+
export const defaultConfig = {
|
|
6
|
+
aliases: { ui: "@/components/ui" },
|
|
7
|
+
tokens: "src/styles/nooeh",
|
|
8
|
+
version: configVersion,
|
|
9
|
+
};
|
|
10
|
+
export async function hasConfig(projectDirectory) {
|
|
11
|
+
try {
|
|
12
|
+
await access(resolve(projectDirectory, configFileName));
|
|
13
|
+
return true;
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
function ensureRelativePath(projectDirectory, path, name) {
|
|
20
|
+
const absolutePath = resolve(projectDirectory, path);
|
|
21
|
+
const relativePath = relative(projectDirectory, absolutePath);
|
|
22
|
+
if (isAbsolute(relativePath) || relativePath.startsWith("..")) {
|
|
23
|
+
throw new Error(`${name} must be inside the project directory.`);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export function validateConfig(config) {
|
|
27
|
+
const candidate = config;
|
|
28
|
+
if (!candidate.aliases || typeof candidate.aliases.ui !== "string" || !candidate.aliases.ui) {
|
|
29
|
+
throw new Error("Configure aliases.ui.");
|
|
30
|
+
}
|
|
31
|
+
if (candidate.version !== undefined && candidate.version !== configVersion) {
|
|
32
|
+
throw new Error(`Unsupported nooeh.json version: ${String(candidate.version)}`);
|
|
33
|
+
}
|
|
34
|
+
if (typeof candidate.tokens !== "string" || !candidate.tokens.trim()) {
|
|
35
|
+
throw new Error("Configure tokens.");
|
|
36
|
+
}
|
|
37
|
+
if (isAbsolute(candidate.tokens)) {
|
|
38
|
+
throw new Error("tokens must be inside the project directory.");
|
|
39
|
+
}
|
|
40
|
+
return {
|
|
41
|
+
aliases: { ui: candidate.aliases.ui },
|
|
42
|
+
tokens: candidate.tokens,
|
|
43
|
+
version: configVersion,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function isNotFoundError(error) {
|
|
47
|
+
return typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT";
|
|
48
|
+
}
|
|
49
|
+
export async function readConfig(projectDirectory) {
|
|
50
|
+
const configPath = resolve(projectDirectory, configFileName);
|
|
51
|
+
try {
|
|
52
|
+
const config = JSON.parse(await readFile(configPath, "utf8"));
|
|
53
|
+
return validateConfig(config);
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (isNotFoundError(error)) {
|
|
57
|
+
throw new Error("nooeh.json was not found. Run `nooeh init` first.");
|
|
58
|
+
}
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export async function writeConfig(projectDirectory, config) {
|
|
63
|
+
const validatedConfig = validateConfig(config);
|
|
64
|
+
ensureRelativePath(projectDirectory, validatedConfig.tokens, "tokens");
|
|
65
|
+
const configPath = resolve(projectDirectory, configFileName);
|
|
66
|
+
await writeFile(configPath, `${JSON.stringify(validatedConfig, null, 2)}\n`, "utf8");
|
|
67
|
+
return configPath;
|
|
68
|
+
}
|
|
69
|
+
export function resolveTokensPath(projectDirectory, tokens) {
|
|
70
|
+
ensureRelativePath(projectDirectory, tokens, "tokens");
|
|
71
|
+
return resolve(projectDirectory, tokens);
|
|
72
|
+
}
|
|
73
|
+
function findAliasTarget(alias, paths) {
|
|
74
|
+
for (const [pattern, targets] of Object.entries(paths ?? {})) {
|
|
75
|
+
const target = targets[0];
|
|
76
|
+
if (!target)
|
|
77
|
+
continue;
|
|
78
|
+
const starIndex = pattern.indexOf("*");
|
|
79
|
+
if (starIndex === -1) {
|
|
80
|
+
if (alias === pattern)
|
|
81
|
+
return target;
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const prefix = pattern.slice(0, starIndex);
|
|
85
|
+
const suffix = pattern.slice(starIndex + 1);
|
|
86
|
+
if (!alias.startsWith(prefix) || !alias.endsWith(suffix))
|
|
87
|
+
continue;
|
|
88
|
+
const value = alias.slice(prefix.length, alias.length - suffix.length);
|
|
89
|
+
return target.replaceAll("*", value);
|
|
90
|
+
}
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
export async function resolveAliasPath(projectDirectory, alias) {
|
|
94
|
+
for (const fileName of ["tsconfig.json", "jsconfig.json"]) {
|
|
95
|
+
const configPath = resolve(projectDirectory, fileName);
|
|
96
|
+
try {
|
|
97
|
+
const projectConfig = JSON.parse(await readFile(configPath, "utf8"));
|
|
98
|
+
const compilerOptions = projectConfig.compilerOptions ?? {};
|
|
99
|
+
const aliasTarget = findAliasTarget(alias, compilerOptions.paths);
|
|
100
|
+
if (!aliasTarget)
|
|
101
|
+
continue;
|
|
102
|
+
const baseDirectory = resolve(projectDirectory, compilerOptions.baseUrl ?? ".");
|
|
103
|
+
const resolvedPath = resolve(baseDirectory, aliasTarget);
|
|
104
|
+
ensureRelativePath(projectDirectory, resolvedPath, "UI");
|
|
105
|
+
return resolvedPath;
|
|
106
|
+
}
|
|
107
|
+
catch (error) {
|
|
108
|
+
if (isNotFoundError(error))
|
|
109
|
+
continue;
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
throw new Error(`Could not find the ${alias} alias in compilerOptions.paths of tsconfig.json or jsconfig.json.`);
|
|
114
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function installDependencies(projectDirectory: string, dependencies: readonly string[], isDevelopmentDependency?: boolean): Promise<void>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
function detectPackageManager() {
|
|
3
|
+
const userAgent = process.env.npm_config_user_agent ?? "";
|
|
4
|
+
if (userAgent.startsWith("pnpm"))
|
|
5
|
+
return "pnpm";
|
|
6
|
+
if (userAgent.startsWith("yarn"))
|
|
7
|
+
return "yarn";
|
|
8
|
+
if (userAgent.startsWith("bun"))
|
|
9
|
+
return "bun";
|
|
10
|
+
return "npm";
|
|
11
|
+
}
|
|
12
|
+
export function installDependencies(projectDirectory, dependencies, isDevelopmentDependency = false) {
|
|
13
|
+
if (dependencies.length === 0)
|
|
14
|
+
return Promise.resolve();
|
|
15
|
+
const packageManager = detectPackageManager();
|
|
16
|
+
const developmentFlag = isDevelopmentDependency ? "-D" : undefined;
|
|
17
|
+
const arguments_ = packageManager === "npm"
|
|
18
|
+
? ["install", developmentFlag, ...dependencies].filter((argument) => Boolean(argument))
|
|
19
|
+
: ["add", developmentFlag, ...dependencies].filter((argument) => Boolean(argument));
|
|
20
|
+
return new Promise((resolvePromise, reject) => {
|
|
21
|
+
const child = spawn(packageManager, arguments_, { cwd: projectDirectory, stdio: "inherit" });
|
|
22
|
+
child.on("error", reject);
|
|
23
|
+
child.on("exit", (code) => {
|
|
24
|
+
if (code === 0)
|
|
25
|
+
resolvePromise();
|
|
26
|
+
else
|
|
27
|
+
reject(new Error(`Dependency installation failed with exit code ${code}.`));
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
package/dist/docs.d.ts
ADDED
package/dist/docs.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { registryItems } from "@nooeh/registry";
|
|
2
|
+
function getComponentNames() {
|
|
3
|
+
return Object.keys(registryItems).sort();
|
|
4
|
+
}
|
|
5
|
+
export function list() {
|
|
6
|
+
console.log(getComponentNames().join("\n"));
|
|
7
|
+
}
|
|
8
|
+
export function docs(componentName) {
|
|
9
|
+
if (!componentName) {
|
|
10
|
+
list();
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const item = registryItems[componentName];
|
|
14
|
+
if (!item)
|
|
15
|
+
throw new Error(`Unknown component: ${componentName}`);
|
|
16
|
+
console.log(`${componentName}\n`);
|
|
17
|
+
console.log(`Install: pnpm dlx @nooeh/ui add ${componentName}`);
|
|
18
|
+
console.log(`Files:\n${item.files.map((file) => ` ${file}`).join("\n")}`);
|
|
19
|
+
console.log(`Dependencies:\n${item.dependencies.map((dependency) => ` ${dependency}`).join("\n")}`);
|
|
20
|
+
if (item.registryDependencies.length) {
|
|
21
|
+
console.log(`Also installs:\n${item.registryDependencies.map((dependency) => ` ${dependency}`).join("\n")}`);
|
|
22
|
+
}
|
|
23
|
+
}
|
package/dist/doctor.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function doctor(projectDirectory: string): Promise<boolean>;
|
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { access, readFile } from "node:fs/promises";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { hasConfig, readConfig, resolveAliasPath, resolveTokensPath } from "./config.js";
|
|
4
|
+
async function readPackageJson(projectDirectory) {
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(await readFile(resolve(projectDirectory, "package.json"), "utf8"));
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
async function readProjectFile(projectDirectory, fileName) {
|
|
13
|
+
try {
|
|
14
|
+
return await readFile(resolve(projectDirectory, fileName), "utf8");
|
|
15
|
+
}
|
|
16
|
+
catch {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const entryFileNames = [
|
|
21
|
+
"src/main.tsx",
|
|
22
|
+
"src/main.jsx",
|
|
23
|
+
"src/main.ts",
|
|
24
|
+
"src/main.js",
|
|
25
|
+
"src/index.tsx",
|
|
26
|
+
"src/index.jsx",
|
|
27
|
+
"src/index.ts",
|
|
28
|
+
"src/index.js",
|
|
29
|
+
"src/app/layout.tsx",
|
|
30
|
+
"src/app/layout.jsx",
|
|
31
|
+
"app/layout.tsx",
|
|
32
|
+
"app/layout.jsx",
|
|
33
|
+
];
|
|
34
|
+
async function readEntrySources(projectDirectory) {
|
|
35
|
+
const sources = await Promise.all(entryFileNames.map(async (fileName) => readProjectFile(projectDirectory, fileName)));
|
|
36
|
+
return sources.filter((source) => source !== undefined);
|
|
37
|
+
}
|
|
38
|
+
async function hasStylexCompiler(projectDirectory) {
|
|
39
|
+
const configFileNames = [
|
|
40
|
+
"vite.config.ts",
|
|
41
|
+
"vite.config.mts",
|
|
42
|
+
"vite.config.js",
|
|
43
|
+
"vite.config.mjs",
|
|
44
|
+
"next.config.ts",
|
|
45
|
+
"next.config.mjs",
|
|
46
|
+
"next.config.js",
|
|
47
|
+
"webpack.config.ts",
|
|
48
|
+
"webpack.config.mjs",
|
|
49
|
+
"webpack.config.js",
|
|
50
|
+
"rsbuild.config.ts",
|
|
51
|
+
"rsbuild.config.mjs",
|
|
52
|
+
"rsbuild.config.js",
|
|
53
|
+
"rspack.config.ts",
|
|
54
|
+
"rspack.config.mjs",
|
|
55
|
+
"rspack.config.js",
|
|
56
|
+
];
|
|
57
|
+
const sources = await Promise.all(configFileNames.map(async (fileName) => readProjectFile(projectDirectory, fileName)));
|
|
58
|
+
return sources.some((source) => source?.includes("@stylexjs/unplugin") && source.includes("stylex("));
|
|
59
|
+
}
|
|
60
|
+
function hasDependency(packageJson, dependency) {
|
|
61
|
+
return Boolean(packageJson?.dependencies?.[dependency] ?? packageJson?.devDependencies?.[dependency]);
|
|
62
|
+
}
|
|
63
|
+
export async function doctor(projectDirectory) {
|
|
64
|
+
const checks = [];
|
|
65
|
+
const packageJson = await readPackageJson(projectDirectory);
|
|
66
|
+
const entrySources = await readEntrySources(projectDirectory);
|
|
67
|
+
if (!(await hasConfig(projectDirectory))) {
|
|
68
|
+
checks.push({
|
|
69
|
+
detail: "Run `nooeh init` to configure the default UI alias.",
|
|
70
|
+
name: "nooeh.json",
|
|
71
|
+
status: "warn",
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
else {
|
|
75
|
+
const config = await readConfig(projectDirectory);
|
|
76
|
+
const uiDirectory = await resolveAliasPath(projectDirectory, config.aliases.ui);
|
|
77
|
+
checks.push({
|
|
78
|
+
detail: `${config.aliases.ui} → ${uiDirectory}`,
|
|
79
|
+
name: "UI alias",
|
|
80
|
+
status: "pass",
|
|
81
|
+
});
|
|
82
|
+
const tokenDirectory = resolveTokensPath(projectDirectory, config.tokens);
|
|
83
|
+
const tokenFiles = [
|
|
84
|
+
"color-palette.stylex.ts",
|
|
85
|
+
"tokens.stylex.ts",
|
|
86
|
+
"themes.stylex.ts",
|
|
87
|
+
"theme.ts",
|
|
88
|
+
];
|
|
89
|
+
const hasTokenFiles = await Promise.all(tokenFiles.map(async (fileName) => {
|
|
90
|
+
try {
|
|
91
|
+
await access(resolve(tokenDirectory, fileName));
|
|
92
|
+
return true;
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}));
|
|
98
|
+
checks.push(hasTokenFiles.every(Boolean)
|
|
99
|
+
? {
|
|
100
|
+
detail: `${config.tokens} contains local token sources.`,
|
|
101
|
+
name: "Local tokens",
|
|
102
|
+
status: "pass",
|
|
103
|
+
}
|
|
104
|
+
: {
|
|
105
|
+
detail: `Create local token sources in ${config.tokens} with \`nooeh init --force\`.`,
|
|
106
|
+
name: "Local tokens",
|
|
107
|
+
status: "warn",
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
checks.push(hasDependency(packageJson, "@stylexjs/stylex")
|
|
111
|
+
? { detail: "@stylexjs/stylex is installed.", name: "StyleX runtime", status: "pass" }
|
|
112
|
+
: {
|
|
113
|
+
detail: "It is installed automatically by the first `nooeh add`. Use --skip-dependencies to skip installation.",
|
|
114
|
+
name: "StyleX runtime",
|
|
115
|
+
status: "warn",
|
|
116
|
+
});
|
|
117
|
+
checks.push((await hasStylexCompiler(projectDirectory))
|
|
118
|
+
? {
|
|
119
|
+
detail: "A supported config enables the StyleX compiler.",
|
|
120
|
+
name: "StyleX compiler",
|
|
121
|
+
status: "pass",
|
|
122
|
+
}
|
|
123
|
+
: {
|
|
124
|
+
detail: "Configure the StyleX compiler for your bundler. Vite users can run `nooeh init --framework vite`.",
|
|
125
|
+
name: "StyleX compiler",
|
|
126
|
+
status: "warn",
|
|
127
|
+
});
|
|
128
|
+
checks.push(entrySources.some((source) => source.includes("./styles/nooeh.css") || source.includes("@nooeh/ui/global.css"))
|
|
129
|
+
? {
|
|
130
|
+
detail: "An application entry imports nooeh global CSS.",
|
|
131
|
+
name: "Global CSS",
|
|
132
|
+
status: "pass",
|
|
133
|
+
}
|
|
134
|
+
: {
|
|
135
|
+
detail: "Import `src/styles/nooeh.css` from an application entry point.",
|
|
136
|
+
name: "Global CSS",
|
|
137
|
+
status: "warn",
|
|
138
|
+
});
|
|
139
|
+
checks.push(entrySources.some((source) => source.includes("applyNooehTheme()"))
|
|
140
|
+
? {
|
|
141
|
+
detail: "An application entry applies a nooeh theme.",
|
|
142
|
+
name: "Theme application",
|
|
143
|
+
status: "pass",
|
|
144
|
+
}
|
|
145
|
+
: {
|
|
146
|
+
detail: "Call applyNooehTheme() before rendering your application.",
|
|
147
|
+
name: "Theme application",
|
|
148
|
+
status: "warn",
|
|
149
|
+
});
|
|
150
|
+
checks.push(hasDependency(packageJson, "@stylexjs/unplugin")
|
|
151
|
+
? {
|
|
152
|
+
detail: "@stylexjs/unplugin is installed.",
|
|
153
|
+
name: "StyleX build plugin",
|
|
154
|
+
status: "pass",
|
|
155
|
+
}
|
|
156
|
+
: {
|
|
157
|
+
detail: "Add @stylexjs/unplugin to your bundler configuration, such as Vite or esbuild.",
|
|
158
|
+
name: "StyleX build plugin",
|
|
159
|
+
status: "warn",
|
|
160
|
+
});
|
|
161
|
+
for (const check of checks) {
|
|
162
|
+
console.log(`${check.status === "pass" ? "✓" : "!"} ${check.name}: ${check.detail}`);
|
|
163
|
+
}
|
|
164
|
+
return checks.every((check) => check.status === "pass");
|
|
165
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function run(arguments_?: string[]): Promise<boolean>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
import { add } from "./add.js";
|
|
3
|
+
import { parseArguments } from "./arguments.js";
|
|
4
|
+
import { doctor } from "./doctor.js";
|
|
5
|
+
import { docs, list } from "./docs.js";
|
|
6
|
+
import { init } from "./init.js";
|
|
7
|
+
import { newComponent } from "./new-component.js";
|
|
8
|
+
export async function run(arguments_ = process.argv.slice(2)) {
|
|
9
|
+
const { command, options, positionals } = parseArguments(arguments_);
|
|
10
|
+
const projectDirectory = resolve(String(options.cwd ?? process.cwd()));
|
|
11
|
+
try {
|
|
12
|
+
if (command === "init") {
|
|
13
|
+
await init(projectDirectory, options);
|
|
14
|
+
}
|
|
15
|
+
else if (command === "add") {
|
|
16
|
+
await add(projectDirectory, positionals[0], options);
|
|
17
|
+
}
|
|
18
|
+
else if (command === "doctor") {
|
|
19
|
+
await doctor(projectDirectory);
|
|
20
|
+
}
|
|
21
|
+
else if (command === "docs") {
|
|
22
|
+
docs(positionals[0]);
|
|
23
|
+
}
|
|
24
|
+
else if (command === "list") {
|
|
25
|
+
list();
|
|
26
|
+
}
|
|
27
|
+
else if (command === "new") {
|
|
28
|
+
await newComponent(projectDirectory, positionals[0]);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
console.log(`Nooeh CLI
|
|
32
|
+
|
|
33
|
+
Usage:
|
|
34
|
+
nooeh init
|
|
35
|
+
nooeh init --framework vite
|
|
36
|
+
nooeh add <component> [--skip-dependencies] [--dry-run]
|
|
37
|
+
nooeh doctor
|
|
38
|
+
nooeh list
|
|
39
|
+
nooeh docs [component]
|
|
40
|
+
nooeh new <kebab-case-name>`);
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
package/dist/init.d.ts
ADDED
package/dist/init.js
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
3
|
+
import { createInterface } from "node:readline/promises";
|
|
4
|
+
import { configFileName, defaultConfig, hasConfig, resolveTokensPath, writeConfig, } from "./config.js";
|
|
5
|
+
import { installDependencies } from "./dependencies.js";
|
|
6
|
+
import { getTokenFiles } from "@nooeh/registry";
|
|
7
|
+
async function ask(question, defaultValue, readline) {
|
|
8
|
+
const answer = await readline.question(`${question} (${defaultValue}) `);
|
|
9
|
+
return answer.trim() || defaultValue;
|
|
10
|
+
}
|
|
11
|
+
function getSourceDirectory(projectDirectory) {
|
|
12
|
+
return join(projectDirectory, "src");
|
|
13
|
+
}
|
|
14
|
+
async function readViteConfig(projectDirectory) {
|
|
15
|
+
const configFileNames = [
|
|
16
|
+
"vite.config.ts",
|
|
17
|
+
"vite.config.mts",
|
|
18
|
+
"vite.config.js",
|
|
19
|
+
"vite.config.mjs",
|
|
20
|
+
];
|
|
21
|
+
for (const fileName of configFileNames) {
|
|
22
|
+
const path = join(projectDirectory, fileName);
|
|
23
|
+
try {
|
|
24
|
+
await access(path);
|
|
25
|
+
return { path, source: await readFile(path, "utf8") };
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
if (typeof error === "object" &&
|
|
29
|
+
error !== null &&
|
|
30
|
+
"code" in error &&
|
|
31
|
+
error.code === "ENOENT") {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
throw error;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
throw new Error("Could not find a Vite config file.");
|
|
38
|
+
}
|
|
39
|
+
function configureVite(source) {
|
|
40
|
+
const importLine = 'import stylex from "@stylexjs/unplugin/vite";';
|
|
41
|
+
const pluginPattern = /plugins:\s*\[([^\]]*)\]/s;
|
|
42
|
+
if (source.includes("stylex("))
|
|
43
|
+
return source;
|
|
44
|
+
if (!pluginPattern.test(source)) {
|
|
45
|
+
throw new Error("Could not safely update the Vite plugins array. Add stylex({ useCSSLayers: true }) manually.");
|
|
46
|
+
}
|
|
47
|
+
const withImport = source.includes(importLine) ? source : `${importLine}\n${source}`;
|
|
48
|
+
return withImport.replace(pluginPattern, (_, plugins) => {
|
|
49
|
+
const prefix = plugins.trim()
|
|
50
|
+
? `stylex({ useCSSLayers: true }), ${plugins}`
|
|
51
|
+
: "stylex({ useCSSLayers: true })";
|
|
52
|
+
return `plugins: [${prefix}]`;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
function toModuleSpecifier(fromDirectory, path) {
|
|
56
|
+
const source = relative(fromDirectory, path).replace(/\.ts$/, "").split(sep).join("/");
|
|
57
|
+
return source.startsWith(".") ? source : `./${source}`;
|
|
58
|
+
}
|
|
59
|
+
async function writeViteFiles(projectDirectory, tokenDirectory) {
|
|
60
|
+
const { path: configPath, source: configSource } = await readViteConfig(projectDirectory);
|
|
61
|
+
const configuredVite = configureVite(configSource);
|
|
62
|
+
const { entryPath, stylePath, themePath } = getNooehPaths(projectDirectory, tokenDirectory);
|
|
63
|
+
const entrySource = await readFile(entryPath, "utf8");
|
|
64
|
+
const styleImport = 'import "./styles/nooeh.css";';
|
|
65
|
+
const themeImport = `import { applyNooehTheme } from "${toModuleSpecifier(dirname(entryPath), themePath)}";`;
|
|
66
|
+
const themeApply = "applyNooehTheme();";
|
|
67
|
+
const imports = [styleImport, themeImport].filter((line) => !entrySource.includes(line));
|
|
68
|
+
const nextEntrySource = `${imports.join("\n")}\n${entrySource}`;
|
|
69
|
+
await writeNooehFiles(projectDirectory, tokenDirectory);
|
|
70
|
+
await writeFile(entryPath, nextEntrySource.includes(themeApply)
|
|
71
|
+
? nextEntrySource
|
|
72
|
+
: `${imports.length ? `${imports.join("\n")}\n` : ""}${themeApply}\n${entrySource}`, "utf8");
|
|
73
|
+
await writeFile(configPath, configuredVite, "utf8");
|
|
74
|
+
console.log(`Configured Vite and created ${relative(projectDirectory, stylePath)}.`);
|
|
75
|
+
}
|
|
76
|
+
function getNooehPaths(projectDirectory, tokenDirectory) {
|
|
77
|
+
const sourceDirectory = getSourceDirectory(projectDirectory);
|
|
78
|
+
return {
|
|
79
|
+
entryPath: join(sourceDirectory, "main.tsx"),
|
|
80
|
+
stylePath: join(sourceDirectory, "styles", "nooeh.css"),
|
|
81
|
+
themePath: join(tokenDirectory, "theme.ts"),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
async function writeNooehFiles(projectDirectory, tokenDirectory) {
|
|
85
|
+
const { entryPath, stylePath, themePath } = getNooehPaths(projectDirectory, tokenDirectory);
|
|
86
|
+
await mkdir(dirname(stylePath), { recursive: true });
|
|
87
|
+
await mkdir(tokenDirectory, { recursive: true });
|
|
88
|
+
await writeFileIfMissing(stylePath, '@import "@nooeh/ui/global.css";\n');
|
|
89
|
+
for (const file of await getTokenFiles()) {
|
|
90
|
+
await writeFileIfMissing(join(tokenDirectory, file.name), file.content);
|
|
91
|
+
}
|
|
92
|
+
await writeFileIfMissing(themePath, `import { darkColorTheme, darkShadowTheme, lightColorTheme, lightShadowTheme } from "./themes.stylex";\nimport * as stylex from "@stylexjs/stylex";\n\nexport type NooehColorMode = "light" | "dark";\n\nconst themeClassNames = [\n stylex.props(lightColorTheme, lightShadowTheme).className,\n stylex.props(darkColorTheme, darkShadowTheme).className,\n]\n .filter(Boolean)\n .flatMap((className) => className.split(" "));\n\nexport function applyNooehTheme(mode: NooehColorMode = "light") {\n const colorTheme = mode === "dark" ? darkColorTheme : lightColorTheme;\n const shadowTheme = mode === "dark" ? darkShadowTheme : lightShadowTheme;\n const themeClassName = stylex.props(colorTheme, shadowTheme).className ?? "";\n const root = document.documentElement;\n\n root.dataset.theme = mode;\n root.classList.remove(...themeClassNames);\n root.classList.add(...themeClassName.split(" ").filter(Boolean));\n}\n`);
|
|
93
|
+
return { entryPath, stylePath, themePath };
|
|
94
|
+
}
|
|
95
|
+
async function writeFileIfMissing(path, source) {
|
|
96
|
+
try {
|
|
97
|
+
await access(path);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
await writeFile(path, source, "utf8");
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
export async function init(projectDirectory, options) {
|
|
104
|
+
if ((await hasConfig(projectDirectory)) && !options.force) {
|
|
105
|
+
throw new Error("nooeh.json already exists. Use --force to create it again.");
|
|
106
|
+
}
|
|
107
|
+
const isInteractive = process.stdin.isTTY && process.stdout.isTTY && !options.defaults;
|
|
108
|
+
const readline = isInteractive
|
|
109
|
+
? createInterface({ input: process.stdin, output: process.stdout })
|
|
110
|
+
: null;
|
|
111
|
+
try {
|
|
112
|
+
if (options.framework && options.framework !== "vite") {
|
|
113
|
+
throw new Error(`Unsupported framework: ${options.framework}. Use vite or omit --framework.`);
|
|
114
|
+
}
|
|
115
|
+
const uiAlias = options["ui-alias"] ??
|
|
116
|
+
(readline
|
|
117
|
+
? await ask("Enter the UI alias.", defaultConfig.aliases.ui, readline)
|
|
118
|
+
: defaultConfig.aliases.ui);
|
|
119
|
+
const tokens = options.tokens ??
|
|
120
|
+
(readline
|
|
121
|
+
? await ask("Enter the token directory.", defaultConfig.tokens, readline)
|
|
122
|
+
: defaultConfig.tokens);
|
|
123
|
+
const tokenDirectory = resolveTokensPath(projectDirectory, tokens);
|
|
124
|
+
if (!options["skip-dependencies"]) {
|
|
125
|
+
await installDependencies(projectDirectory, ["@stylexjs/stylex"]);
|
|
126
|
+
await installDependencies(projectDirectory, ["@stylexjs/unplugin"], true);
|
|
127
|
+
}
|
|
128
|
+
if (options.framework === "vite") {
|
|
129
|
+
await writeViteFiles(projectDirectory, tokenDirectory);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
const { stylePath, themePath } = await writeNooehFiles(projectDirectory, tokenDirectory);
|
|
133
|
+
console.log(`Created ${relative(projectDirectory, stylePath)} and ${relative(projectDirectory, themePath)}.`);
|
|
134
|
+
console.log("Import the CSS and call applyNooehTheme() from your application entry point.");
|
|
135
|
+
}
|
|
136
|
+
await writeConfig(projectDirectory, { aliases: { ui: uiAlias }, tokens });
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
readline?.close();
|
|
140
|
+
}
|
|
141
|
+
console.log(`Created configuration: ${configFileName}`);
|
|
142
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function newComponent(projectDirectory: string, name: string | undefined): Promise<void>;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { access, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import { dirname, join, relative, sep } from "node:path";
|
|
3
|
+
import { readConfig, resolveAliasPath, resolveTokensPath } from "./config.js";
|
|
4
|
+
function toPascalCase(name) {
|
|
5
|
+
return name
|
|
6
|
+
.split("-")
|
|
7
|
+
.filter(Boolean)
|
|
8
|
+
.map((part) => `${part[0]?.toUpperCase()}${part.slice(1)}`)
|
|
9
|
+
.join("");
|
|
10
|
+
}
|
|
11
|
+
export async function newComponent(projectDirectory, name) {
|
|
12
|
+
if (!name || !/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(name)) {
|
|
13
|
+
throw new Error("Enter a component name in kebab-case. For example: status-chip.");
|
|
14
|
+
}
|
|
15
|
+
const config = await readConfig(projectDirectory);
|
|
16
|
+
const uiDirectory = await resolveAliasPath(projectDirectory, config.aliases.ui);
|
|
17
|
+
const componentName = toPascalCase(name);
|
|
18
|
+
const componentPath = join(uiDirectory, `${name}.tsx`);
|
|
19
|
+
const tokenPath = join(resolveTokensPath(projectDirectory, config.tokens), "tokens.stylex.ts");
|
|
20
|
+
const relativeTokenPath = relative(dirname(componentPath), tokenPath)
|
|
21
|
+
.replace(/\.ts$/, "")
|
|
22
|
+
.split(sep)
|
|
23
|
+
.join("/");
|
|
24
|
+
const tokenImport = relativeTokenPath.startsWith(".")
|
|
25
|
+
? relativeTokenPath
|
|
26
|
+
: `./${relativeTokenPath}`;
|
|
27
|
+
const files = [componentPath];
|
|
28
|
+
for (const file of files) {
|
|
29
|
+
try {
|
|
30
|
+
await access(file);
|
|
31
|
+
throw new Error(`${file} already exists. Existing components will not be overwritten.`);
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
if (!(error instanceof Error) || !error.message.includes("already exists"))
|
|
35
|
+
continue;
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
await mkdir(uiDirectory, { recursive: true });
|
|
40
|
+
await writeFile(componentPath, `import { colorVars, radiusVars, spacingVars } from "${tokenImport}";\nimport * as stylex from "@stylexjs/stylex";\nimport type { ComponentProps } from "react";\n\nconst styles = stylex.create({\n root: {\n backgroundColor: colorVars.bgSurface,\n borderRadius: radiusVars.sm,\n padding: spacingVars.space3,\n },\n});\n\nexport type ${componentName}Props = ComponentProps<"div"> & {\n xstyle?: stylex.StyleXStyles;\n};\n\nexport function ${componentName}({ className, style, xstyle, ...props }: ${componentName}Props) {\n const stylexProps = stylex.props(styles.root, xstyle);\n\n return (\n <div\n {...props}\n className={[stylexProps.className, className].filter(Boolean).join(" ")}\n style={{ ...stylexProps.style, ...style }}\n />\n );\n}\n`, { flag: "wx" });
|
|
41
|
+
console.log(`Created a ${name} component scaffold: ${componentPath}`);
|
|
42
|
+
}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { dependencyVersions, getRegistryItem } from "@nooeh/registry";
|
|
2
|
+
export async function resolveComponent(name) {
|
|
3
|
+
if (URL.canParse(name))
|
|
4
|
+
return resolveRemoteComponent(name);
|
|
5
|
+
const components = new Set();
|
|
6
|
+
const externalDependencies = new Set();
|
|
7
|
+
const files = new Map();
|
|
8
|
+
async function visit(componentName) {
|
|
9
|
+
if (components.has(componentName))
|
|
10
|
+
return;
|
|
11
|
+
const item = await getRegistryItem(componentName);
|
|
12
|
+
components.add(componentName);
|
|
13
|
+
for (const file of item.files)
|
|
14
|
+
files.set(file.path, file);
|
|
15
|
+
for (const dependency of item.dependencies)
|
|
16
|
+
externalDependencies.add(dependency);
|
|
17
|
+
for (const dependency of item.registryDependencies) {
|
|
18
|
+
await visit(dependency);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
await visit(name);
|
|
22
|
+
return {
|
|
23
|
+
components: [...components],
|
|
24
|
+
files: [...files.values()],
|
|
25
|
+
externalDependencies: [...externalDependencies].map((dependency) => `${dependency}@${dependencyVersions[dependency] ?? "latest"}`),
|
|
26
|
+
primaryExport: (await getRegistryItem(name)).primaryExport,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async function resolveRemoteComponent(url) {
|
|
30
|
+
const components = new Set();
|
|
31
|
+
const externalDependencies = new Set();
|
|
32
|
+
const files = new Map();
|
|
33
|
+
async function visitLocal(name) {
|
|
34
|
+
if (components.has(name))
|
|
35
|
+
return;
|
|
36
|
+
const item = await getRegistryItem(name);
|
|
37
|
+
components.add(name);
|
|
38
|
+
for (const file of item.files)
|
|
39
|
+
files.set(file.path, file);
|
|
40
|
+
for (const dependency of item.dependencies)
|
|
41
|
+
externalDependencies.add(dependency);
|
|
42
|
+
for (const dependency of item.registryDependencies)
|
|
43
|
+
await visitLocal(dependency);
|
|
44
|
+
}
|
|
45
|
+
async function visitRemote(itemUrl) {
|
|
46
|
+
const response = await fetch(itemUrl);
|
|
47
|
+
if (!response.ok)
|
|
48
|
+
throw new Error(`Could not load the registry: ${response.status}`);
|
|
49
|
+
const item = (await response.json());
|
|
50
|
+
if (!Array.isArray(item.files) ||
|
|
51
|
+
!Array.isArray(item.dependencies) ||
|
|
52
|
+
!item.files.every((file) => typeof file === "object" &&
|
|
53
|
+
file !== null &&
|
|
54
|
+
typeof file.path === "string" &&
|
|
55
|
+
typeof file.content === "string")) {
|
|
56
|
+
throw new Error("This is not a valid Nooeh registry item.");
|
|
57
|
+
}
|
|
58
|
+
const itemName = item.name ?? itemUrl;
|
|
59
|
+
if (components.has(itemName))
|
|
60
|
+
return;
|
|
61
|
+
components.add(itemName);
|
|
62
|
+
for (const file of item.files)
|
|
63
|
+
files.set(file.path, file);
|
|
64
|
+
for (const dependency of item.dependencies)
|
|
65
|
+
externalDependencies.add(dependency);
|
|
66
|
+
for (const dependency of item.registryDependencies ?? []) {
|
|
67
|
+
if (URL.canParse(dependency))
|
|
68
|
+
await visitRemote(dependency);
|
|
69
|
+
else
|
|
70
|
+
await visitLocal(dependency);
|
|
71
|
+
}
|
|
72
|
+
return item.primaryExport ?? item.name ?? "Component";
|
|
73
|
+
}
|
|
74
|
+
const primaryExport = (await visitRemote(url)) ?? "Component";
|
|
75
|
+
return {
|
|
76
|
+
components: [...components],
|
|
77
|
+
externalDependencies: [...externalDependencies].map((dependency) => `${dependency}@${dependencyVersions[dependency] ?? "latest"}`),
|
|
78
|
+
files: [...files.values()],
|
|
79
|
+
primaryExport,
|
|
80
|
+
};
|
|
81
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@nooeh/cli",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"bin": {
|
|
6
|
+
"nooeh": "./dist/cli.js"
|
|
7
|
+
},
|
|
8
|
+
"files": [
|
|
9
|
+
"dist"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@nooeh/registry": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^22.20.1",
|
|
26
|
+
"typescript": "7.0.2"
|
|
27
|
+
},
|
|
28
|
+
"scripts": {
|
|
29
|
+
"build": "pnpm --filter @nooeh/registry build && tsc -p tsconfig.build.json",
|
|
30
|
+
"test": "pnpm build && node --experimental-strip-types --test test/*.test.ts",
|
|
31
|
+
"typecheck": "tsc --noEmit && pnpm build && tsc -p tsconfig.test.json"
|
|
32
|
+
}
|
|
33
|
+
}
|