@nooeh/cli 0.2.4 → 0.2.6
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/dist/add.js +8 -4
- package/dist/init.js +25 -34
- package/package.json +2 -2
package/dist/add.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
2
|
+
import { dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
3
3
|
import { createInterface } from "node:readline/promises";
|
|
4
4
|
import { defaultConfig, hasConfig, readConfig, resolveConfigAlias } from "./config.js";
|
|
5
5
|
import { installDependencies } from "./dependencies.js";
|
|
@@ -49,8 +49,11 @@ function resolveTargetPath(uiDirectory, filePath) {
|
|
|
49
49
|
}
|
|
50
50
|
return targetPath;
|
|
51
51
|
}
|
|
52
|
-
function replaceTokenImport(source,
|
|
53
|
-
|
|
52
|
+
function replaceTokenImport(source, targetPath, stylesDirectory) {
|
|
53
|
+
const tokenPath = join(stylesDirectory, "semantic.stylex");
|
|
54
|
+
const importPath = relative(dirname(targetPath), tokenPath).split(sep).join("/");
|
|
55
|
+
const relativeImportPath = importPath.startsWith(".") ? importPath : `./${importPath}`;
|
|
56
|
+
return source.replaceAll("@nooeh/tokens/semantic.stylex", relativeImportPath);
|
|
54
57
|
}
|
|
55
58
|
export async function add(projectDirectory, componentName, options = {}) {
|
|
56
59
|
if (!componentName)
|
|
@@ -65,6 +68,7 @@ export async function add(projectDirectory, componentName, options = {}) {
|
|
|
65
68
|
const config = options["dry-run"] && shouldInitialize ? defaultConfig : await readConfig(projectDirectory);
|
|
66
69
|
const resolved = await resolveComponent(componentName);
|
|
67
70
|
const uiDirectory = await resolveConfigAlias(projectDirectory, config.aliases.ui, "aliases.ui");
|
|
71
|
+
const stylesDirectory = await resolveConfigAlias(projectDirectory, config.aliases.styles, "aliases.styles");
|
|
68
72
|
let isOverwriteConfirmed = false;
|
|
69
73
|
async function confirmOverwrite() {
|
|
70
74
|
if (isOverwriteConfirmed)
|
|
@@ -78,7 +82,7 @@ export async function add(projectDirectory, componentName, options = {}) {
|
|
|
78
82
|
console.log(`Will add: ${targetPath}`);
|
|
79
83
|
continue;
|
|
80
84
|
}
|
|
81
|
-
await writeSource(replaceTokenImport(file.content,
|
|
85
|
+
await writeSource(replaceTokenImport(file.content, targetPath, stylesDirectory), targetPath, confirmOverwrite);
|
|
82
86
|
}
|
|
83
87
|
const shouldInstallDependencies = resolved.externalDependencies.length > 0 &&
|
|
84
88
|
!options.skipDependencyInstall &&
|
package/dist/init.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
-
import { join, relative
|
|
2
|
+
import { join, relative } from "node:path";
|
|
3
3
|
import { createInterface } from "node:readline/promises";
|
|
4
4
|
import { configFileName, defaultConfig, hasConfig, resolveConfigAlias, writeConfig, } from "./config.js";
|
|
5
5
|
import { installDependencies } from "./dependencies.js";
|
|
@@ -33,40 +33,31 @@ async function readViteConfig(projectDirectory) {
|
|
|
33
33
|
}
|
|
34
34
|
throw new Error("Could not find a Vite config file.");
|
|
35
35
|
}
|
|
36
|
-
function
|
|
37
|
-
const tokenPath = relative(projectDirectory, tokenDirectory).split(sep).join("/");
|
|
38
|
-
const aliasPattern = `${stylesAlias}/*`;
|
|
39
|
-
const aliasTarget = `/ROOT/${tokenPath}/*`;
|
|
40
|
-
return `stylex.vite({
|
|
41
|
-
aliases: { ${JSON.stringify(aliasPattern)}: [${JSON.stringify(aliasTarget)}] },
|
|
42
|
-
unstable_moduleResolution: {
|
|
43
|
-
type: "commonJS",
|
|
44
|
-
rootDir: new URL(".", import.meta.url).pathname,
|
|
45
|
-
},
|
|
46
|
-
})`;
|
|
47
|
-
}
|
|
48
|
-
function configureVite(source, stylesAlias, tokenDirectory, projectDirectory) {
|
|
36
|
+
function configureVite(source) {
|
|
49
37
|
const importLine = 'import stylex from "@stylexjs/unplugin";';
|
|
50
38
|
const pluginPattern = /plugins:\s*\[([^\]]*)\]/s;
|
|
51
|
-
const
|
|
52
|
-
const stylexPlugin = getStylexPlugin(stylesAlias, tokenDirectory, projectDirectory);
|
|
39
|
+
const stylexPlugin = "stylex.vite()";
|
|
53
40
|
const legacyStylexPluginPattern = /stylex\.vite\(\{\s*useCSSLayers:\s*true\s*\}\)/;
|
|
54
41
|
const emptyStylexPluginPattern = /stylex\.vite\(\)/;
|
|
42
|
+
const nooehStylexPluginPattern = /stylex\.vite\(\{\s*(?:\/\/[^\n]*\s*)?(?:useCSSLayers:\s*true,?\s*)?aliases:\s*\{[^}]*\},\s*unstable_moduleResolution:\s*\{\s*type:\s*["']commonJS["'],\s*rootDir:\s*new URL\(["']\.["'],\s*import\.meta\.url\)\.pathname,?\s*\},\s*\}\)/s;
|
|
55
43
|
if (!source.includes("stylex.vite(") && !pluginPattern.test(source)) {
|
|
56
44
|
throw new Error("Could not safely update the Vite plugins array. Add stylex.vite() manually.");
|
|
57
45
|
}
|
|
58
|
-
|
|
46
|
+
if (nooehStylexPluginPattern.test(source)) {
|
|
47
|
+
return source.replace(nooehStylexPluginPattern, stylexPlugin);
|
|
48
|
+
}
|
|
59
49
|
if (source.includes("unstable_moduleResolution")) {
|
|
60
|
-
|
|
50
|
+
throw new Error("Could not safely simplify the existing StyleX plugin. Remove Nooeh's aliases and unstable_moduleResolution manually, then use stylex.vite().");
|
|
61
51
|
}
|
|
62
|
-
|
|
52
|
+
let withStylex = source;
|
|
53
|
+
if (legacyStylexPluginPattern.test(source)) {
|
|
63
54
|
withStylex = source.replace(legacyStylexPluginPattern, stylexPlugin);
|
|
64
55
|
}
|
|
65
56
|
else if (emptyStylexPluginPattern.test(source)) {
|
|
66
57
|
withStylex = source.replace(emptyStylexPluginPattern, stylexPlugin);
|
|
67
58
|
}
|
|
68
59
|
else if (source.includes("stylex.vite(")) {
|
|
69
|
-
throw new Error("Could not safely update the existing StyleX plugin.
|
|
60
|
+
throw new Error("Could not safely update the existing StyleX plugin. Configure stylex.vite() manually.");
|
|
70
61
|
}
|
|
71
62
|
else {
|
|
72
63
|
withStylex = (source.includes(importLine) ? source : `${importLine}\n${source}`).replace(pluginPattern, (_, plugins) => {
|
|
@@ -74,16 +65,11 @@ function configureVite(source, stylesAlias, tokenDirectory, projectDirectory) {
|
|
|
74
65
|
return `plugins: [${prefix}]`;
|
|
75
66
|
});
|
|
76
67
|
}
|
|
77
|
-
|
|
78
|
-
return withStylex;
|
|
79
|
-
if (!configPattern.test(withStylex)) {
|
|
80
|
-
throw new Error('Could not safely add the "@" Vite alias. Add resolve.alias["@"] manually.');
|
|
81
|
-
}
|
|
82
|
-
return withStylex.replace(configPattern, 'defineConfig({\n resolve: { alias: { "@": new URL("./src", import.meta.url).pathname } },\n ');
|
|
68
|
+
return withStylex;
|
|
83
69
|
}
|
|
84
|
-
async function writeViteFiles(projectDirectory, tokenDirectory,
|
|
70
|
+
async function writeViteFiles(projectDirectory, tokenDirectory, refreshLegacyTokens) {
|
|
85
71
|
const { path: configPath, source: configSource } = await readViteConfig(projectDirectory);
|
|
86
|
-
const configuredVite = configureVite(configSource
|
|
72
|
+
const configuredVite = configureVite(configSource);
|
|
87
73
|
await writeNooehFiles(projectDirectory, tokenDirectory, refreshLegacyTokens);
|
|
88
74
|
await writeFile(configPath, configuredVite, "utf8");
|
|
89
75
|
console.log(`Configured Vite and created ${relative(projectDirectory, tokenDirectory)}.`);
|
|
@@ -91,16 +77,21 @@ async function writeViteFiles(projectDirectory, tokenDirectory, stylesAlias, ref
|
|
|
91
77
|
async function writeNooehFiles(projectDirectory, tokenDirectory, refreshLegacyTokens = false) {
|
|
92
78
|
await mkdir(tokenDirectory, { recursive: true });
|
|
93
79
|
for (const file of await getTokenFiles()) {
|
|
94
|
-
await writeTokenFile(join(tokenDirectory, file.name), file.content, refreshLegacyTokens
|
|
80
|
+
await writeTokenFile(join(tokenDirectory, file.name), file.content, refreshLegacyTokens, file.name);
|
|
95
81
|
}
|
|
96
82
|
}
|
|
97
|
-
function
|
|
98
|
-
|
|
83
|
+
function isLegacyNooehSource(fileName, source) {
|
|
84
|
+
if (fileName === "semantic.stylex.ts") {
|
|
85
|
+
return source.includes('bgCanvas: "initial"') && source.includes('overlay: "initial"');
|
|
86
|
+
}
|
|
87
|
+
return (fileName === "theme-provider.tsx" &&
|
|
88
|
+
source.includes("const styles = stylex.create({") &&
|
|
89
|
+
source.includes("data-theme={resolvedTheme}"));
|
|
99
90
|
}
|
|
100
|
-
async function writeTokenFile(path, source,
|
|
91
|
+
async function writeTokenFile(path, source, shouldRefreshLegacySources, fileName) {
|
|
101
92
|
try {
|
|
102
93
|
const currentSource = await readFile(path, "utf8");
|
|
103
|
-
if (
|
|
94
|
+
if (shouldRefreshLegacySources && isLegacyNooehSource(fileName, currentSource)) {
|
|
104
95
|
await writeFile(path, source, "utf8");
|
|
105
96
|
}
|
|
106
97
|
}
|
|
@@ -138,7 +129,7 @@ export async function init(projectDirectory, options) {
|
|
|
138
129
|
}
|
|
139
130
|
}
|
|
140
131
|
if (options.framework === "vite") {
|
|
141
|
-
await writeViteFiles(projectDirectory, tokenDirectory,
|
|
132
|
+
await writeViteFiles(projectDirectory, tokenDirectory, Boolean(options.force));
|
|
142
133
|
}
|
|
143
134
|
else {
|
|
144
135
|
await writeNooehFiles(projectDirectory, tokenDirectory, Boolean(options.force));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nooeh/cli",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.6",
|
|
4
4
|
"private": false,
|
|
5
5
|
"bin": {
|
|
6
6
|
"nooeh": "./dist/cli.js"
|
|
@@ -19,7 +19,7 @@
|
|
|
19
19
|
"access": "public"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@nooeh/registry": "0.1.
|
|
22
|
+
"@nooeh/registry": "0.1.3"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^22.20.1",
|