Skip to content

Commit a0e7e6c

Browse files
committed
chore(cli): decouple config utility and installer from nx
1 parent 65cecad commit a0e7e6c

4 files changed

Lines changed: 56 additions & 30 deletions

File tree

bin/lib/config.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ import { LEGACY_CONFIG_DEPRECATION } from './desktopConfig.js'
77
const require = createRequire(import.meta.url)
88

99
function loadTsMorph(workspaceRoot) {
10-
const utilPath = join(
11-
workspaceRoot,
12-
'node_modules/@owdproject/nx/dist/utils/utilConfig.js',
13-
)
14-
if (!existsSync(utilPath)) {
15-
throw new Error('Missing @owdproject/nx — run pnpm install at the repo root.')
16-
}
1710
const { Project, SyntaxKind } = require('ts-morph')
1811
return { Project, SyntaxKind }
1912
}
@@ -128,3 +121,53 @@ export function writeDesktopDependencies(packageJsonPath, toAdd, toRemove) {
128121

129122
writeFileSync(packageJsonPath, JSON.stringify(pkg, null, 2) + '\n')
130123
}
124+
125+
export function addToDesktopConfig(configPath, workspaceRoot, key, value) {
126+
const { Project, SyntaxKind } = loadTsMorph(workspaceRoot)
127+
const project = new Project()
128+
const sourceFile = project.addSourceFileAtPath(configPath)
129+
const configObj = getConfigObject(sourceFile, SyntaxKind)
130+
131+
const rawProp = configObj.getProperty(key)
132+
const targetProp = rawProp?.asKind(SyntaxKind.PropertyAssignment)
133+
134+
if (!targetProp) {
135+
// if property doesn't exist, create it
136+
configObj.addPropertyAssignment({
137+
name: key,
138+
initializer: key === 'theme' ? `'${value}'` : `[ '${value}' ]`,
139+
})
140+
141+
sourceFile.saveSync()
142+
console.log(`🛠️ Created '${key}' and added '${value}'`)
143+
return
144+
}
145+
146+
// handle 'theme' key — just set string literal
147+
if (key === 'theme') {
148+
targetProp.setInitializer(`'${value}'`)
149+
sourceFile.saveSync()
150+
console.log(`🎨 Theme set to '${value}'`)
151+
return
152+
}
153+
154+
// ensure initializer is an array
155+
const initializer = targetProp.getInitializerIfKind(SyntaxKind.ArrayLiteralExpression)
156+
if (!initializer) {
157+
throw new Error(`Expected '${key}' to be an array`)
158+
}
159+
160+
const exists = initializer.getElements().some((el) => {
161+
const text = el.getText().trim().replace(/['"`]/g, '')
162+
return text === value
163+
})
164+
165+
if (exists) {
166+
console.log(`⚠️ '${value}' already exists in '${key}'`)
167+
return
168+
}
169+
170+
initializer.addElement(`'${value}'`)
171+
sourceFile.saveSync()
172+
console.log(`✅ Added '${value}' to '${key}'`)
173+
}

bin/lib/create.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
resolveDesktopConfigPath,
99
desktopConfigWritePath,
1010
} from './desktopConfig.js'
11+
import { addToDesktopConfig } from './config.js'
1112
import {
1213
linkWorkspacePackage,
1314
spawnAsync,
@@ -231,15 +232,7 @@ export async function runCreateCli(options = {}) {
231232
const resolved = resolveDesktopConfigPath(desktopPath)
232233
const configPath = resolved?.path ?? desktopConfigWritePath(desktopPath)
233234

234-
const utilPath = join(
235-
workspaceRoot,
236-
'node_modules/@owdproject/nx/dist/utils/utilConfig.js',
237-
)
238-
if (!existsSync(utilPath)) {
239-
throw new Error('Workspace install needs @owdproject/nx. Run `pnpm install` at the repo root.')
240-
}
241-
const { addToDesktopConfig } = require(utilPath)
242-
addToDesktopConfig(configPath, KINDS[kind].configKey, pkgFullName)
235+
addToDesktopConfig(configPath, workspaceRoot, KINDS[kind].configKey, pkgFullName)
243236

244237
// Run dev:prepare
245238
console.log(`Running dev:prepare on ${pkgFullName}...`)

bin/lib/install.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
resolveDesktopConfigPath,
1818
desktopConfigWritePath,
1919
} from './desktopConfig.js'
20+
import { addToDesktopConfig } from './config.js'
2021

2122
const require = createRequire(import.meta.url)
2223
const __dirname = dirname(fileURLToPath(import.meta.url))
@@ -340,16 +341,7 @@ export async function runPrepareModules(workspaceRoot, _stdio = 'inherit') {
340341
}
341342
}
342343

343-
function loadConfigUtil(workspaceRoot) {
344-
const utilPath = join(
345-
workspaceRoot,
346-
'node_modules/@owdproject/nx/dist/utils/utilConfig.js',
347-
)
348-
if (!existsSync(utilPath)) {
349-
throw new Error('Workspace install needs @owdproject/nx. Run `pnpm install` at the repo root.')
350-
}
351-
return require(utilPath)
352-
}
344+
353345

354346
export function printPlan({ pkgName, kind, targetDir, source, branch, dryRun }) {
355347
const kindMeta = KINDS[kind]
@@ -453,8 +445,7 @@ export async function runWorkspaceInstall({
453445
await cloneRepo(targetDir, source.gitUrl, branch, workspaceRoot)
454446
await linkWorkspacePackage(desktopPath, pkgName)
455447

456-
const { addToDesktopConfig } = loadConfigUtil(workspaceRoot)
457-
addToDesktopConfig(configPath, KINDS[kind].configKey, pkgName)
448+
addToDesktopConfig(configPath, workspaceRoot, KINDS[kind].configKey, pkgName)
458449

459450
await runDevPrepare(workspaceRoot, pkgName)
460451

@@ -464,8 +455,7 @@ export async function runWorkspaceInstall({
464455

465456
export function runNpmInstall(kind, pkgName, workspaceRoot, stdio = 'inherit') {
466457
return new Promise((resolve, reject) => {
467-
const nxTarget = KINDS[kind].nx
468-
const child = spawn('pnpm', ['nx', 'run', nxTarget, `--name=${pkgName}`], {
458+
const child = spawn('node', ['scripts/install-app.mjs', pkgName, `--kind=${kind}`], {
469459
stdio,
470460
shell: true,
471461
cwd: workspaceRoot,

go/owd-cli

11.1 MB
Binary file not shown.

0 commit comments

Comments
 (0)