-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
209 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,29 @@ | ||
{ | ||
"executors": { | ||
"setup-env": { | ||
"implementation": "./src/executors/setup-env/executor", | ||
"schema": "./src/executors/setup-env/schema.json", | ||
"description": "Generate and install test environments in your workspace. Cached and ready for use." | ||
}, | ||
"bootstrap": { | ||
"implementation": "./src/executors/bootstrap/executor", | ||
"schema": "./src/executors/bootstrap/schema.json", | ||
"description": "Bootstraps a test environments in your workspace. Cached and ready for use." | ||
}, | ||
"kill-process": { | ||
"implementation": "./src/executors/kill-process/executor", | ||
"schema": "./src/executors/kill-process/schema.json", | ||
"description": "Kills process by PID, command or file path." | ||
"setup": { | ||
"implementation": "./src/executors/setup/executor", | ||
"schema": "./src/executors/setup/schema.json", | ||
"description": "Generate and install test environments in your workspace. Cached and ready for use." | ||
}, | ||
"npm-publish": { | ||
"implementation": "./src/executors/npm-publish/executor", | ||
"schema": "./src/executors/npm-publish/schema.json", | ||
"description": "Publishes npm packages to a configured registry." | ||
}, | ||
"npm-install": { | ||
"implementation": "./src/executors/npm-install/executor", | ||
"schema": "./src/executors/npm-install/schema.json", | ||
"description": "Installs npm packages in your workspace." | ||
}, | ||
"kill-process": { | ||
"implementation": "./src/executors/kill-process/executor", | ||
"schema": "./src/executors/kill-process/schema.json", | ||
"description": "Kills process by PID, command or file path." | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
tools/build-env/src/executors/internal/normalize-options.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { join } from 'node:path'; | ||
import { DEFAULT_ENVIRONMENTS_OUTPUT_DIR } from '../../internal/constants'; | ||
import { ExecutorContext } from '@nx/devkit'; | ||
|
||
export function normalizeOptions< | ||
T extends ExecutorContext, | ||
I extends Record<string, unknown> | ||
>( | ||
context: T, | ||
options: I | ||
): T & { | ||
options: I & { environmentRoot: string }; | ||
} { | ||
const { projectName } = context; | ||
return { | ||
...context, | ||
options: { | ||
...options, | ||
environmentRoot: join(DEFAULT_ENVIRONMENTS_OUTPUT_DIR, projectName), | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,54 @@ | ||
import { | ||
type ExecutorContext, | ||
readJsonFile, | ||
type TargetConfiguration, | ||
} from '@nx/devkit'; | ||
import { type ExecutorContext, logger, readJsonFile } from '@nx/devkit'; | ||
|
||
import type { NpmInstallExecutorOptions } from './schema'; | ||
import { join, relative } from 'node:path'; | ||
import { executeProcess } from '../../internal/utils/execute-process'; | ||
import { objectToCliArgs } from '../../internal/utils/terminal-command'; | ||
import { PackageJson } from 'nx/src/utils/package-json'; | ||
import { DEFAULT_ENVIRONMENTS_OUTPUT_DIR } from '../../internal/constants'; | ||
import { getBuildOutput } from '../../internal/utils/utils'; | ||
import { normalizeOptions } from '../internal/normalize-options'; | ||
|
||
export type ExecutorOutput = { | ||
export type NpmInstallExecutorOutput = { | ||
success: boolean; | ||
command?: string; | ||
error?: Error; | ||
}; | ||
|
||
const relativeFromPath = (dir: string) => | ||
relative(join(process.cwd(), dir), join(process.cwd())); | ||
|
||
export default async function runNpmInstallExecutor( | ||
terminalAndExecutorOptions: NpmInstallExecutorOptions, | ||
options: NpmInstallExecutorOptions, | ||
context: ExecutorContext | ||
) { | ||
const { projectName, projectsConfigurations } = context; | ||
const { environmentProject = projectName, pkgVersion } = | ||
terminalAndExecutorOptions; | ||
// @TODO DEFAULT_ENVIRONMENTS_OUTPUT_DIR is configured in the registered plugin thing about how to get that value | ||
const environmentRoot = join( | ||
DEFAULT_ENVIRONMENTS_OUTPUT_DIR, | ||
environmentProject | ||
const { | ||
projectName, | ||
projectsConfigurations, | ||
options: opt, | ||
} = normalizeOptions(context, options); | ||
|
||
const packageDistPath = getBuildOutput( | ||
projectsConfigurations.projects[projectName]?.targets['build'] | ||
); | ||
const packageDistPath = getBuildOutput(projectsConfigurations[projectName]); | ||
|
||
const { name: packageName, version } = readJsonFile<PackageJson>( | ||
join(packageDistPath, 'package.json') | ||
); | ||
const userconfig = relativeFromPath( | ||
join(packageDistPath, environmentRoot, '.npmrc') | ||
); | ||
const { pkgVersion = version, environmentRoot } = opt; | ||
|
||
logger.info(`Installing ${packageName}@${pkgVersion} in ${environmentRoot}`); | ||
|
||
await executeProcess({ | ||
command: 'npm', | ||
args: objectToCliArgs({ | ||
_: ['install', `${packageName}@${pkgVersion ?? version}`], | ||
_: ['install', `${packageName}@${pkgVersion}`], | ||
'no-fund': true, | ||
'no-shrinkwrap': true, | ||
save: true, | ||
prefix: environmentRoot, | ||
userconfig, | ||
userconfig: join(environmentRoot, '.npmrc'), | ||
}), | ||
cwd: process.cwd(), | ||
verbose: true, | ||
}); | ||
|
||
return Promise.resolve({ | ||
success: true, | ||
command: 'Installed dependencies successfully.', | ||
} satisfies ExecutorOutput); | ||
} | ||
|
||
function getBuildOutput(target: TargetConfiguration) { | ||
const { options } = target ?? {}; | ||
const { outputPath } = options ?? {}; | ||
if (!outputPath) { | ||
throw new Error('outputPath is required'); | ||
} | ||
return outputPath; | ||
} satisfies NpmInstallExecutorOutput); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { logger, type ExecutorContext } from '@nx/devkit'; | ||
|
||
import type { NpmPublishExecutorOptions } from './schema'; | ||
import { join, relative } from 'node:path'; | ||
import { executeProcess } from '../../internal/utils/execute-process'; | ||
import { objectToCliArgs } from '../../internal/utils/terminal-command'; | ||
import { DEFAULT_ENVIRONMENTS_OUTPUT_DIR } from '../../internal/constants'; | ||
import { getBuildOutput } from '../../internal/utils/utils'; | ||
import { normalizeOptions } from '../internal/normalize-options'; | ||
|
||
export type NpmPublishExecutorOutput = { | ||
success: boolean; | ||
command?: string; | ||
error?: Error; | ||
}; | ||
|
||
const relativeFromDist = (dir: string) => | ||
relative(join(process.cwd(), dir), join(process.cwd())); | ||
|
||
export default async function runNpmPublishExecutor( | ||
options: NpmPublishExecutorOptions, | ||
context: ExecutorContext | ||
) { | ||
const { | ||
projectName, | ||
projectsConfigurations, | ||
options: opt, | ||
} = normalizeOptions(context, options); | ||
const { environmentRoot } = opt; | ||
|
||
const { targets } = projectsConfigurations.projects[projectName]; | ||
const packageDistPath = getBuildOutput(targets['build']); | ||
const userconfig = join( | ||
relativeFromDist(packageDistPath), | ||
join(environmentRoot, '.npmrc') | ||
); | ||
|
||
logger.info( | ||
`Publishing package from ${packageDistPath} to ${environmentRoot} with userconfig ${userconfig}` | ||
); | ||
|
||
// @TODO: try leverage nx-release-publish | ||
await executeProcess({ | ||
command: 'npm', | ||
args: objectToCliArgs({ | ||
_: ['publish'], | ||
userconfig, | ||
}), | ||
cwd: packageDistPath, | ||
verbose: true, | ||
}); | ||
|
||
return Promise.resolve({ | ||
success: true, | ||
command: 'Published package successfully.', | ||
} satisfies NpmPublishExecutorOutput); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"$schema": "http://json-schema.org/schema", | ||
"$id": "KillProcessExecutorOptions", | ||
"title": "A executor to kill processes by PID, command, or file", | ||
"type": "object", | ||
"properties": { | ||
"dryRun": { | ||
"type": "boolean", | ||
"description": "Print the commands that would be run, but don't actually run them", | ||
"default": false | ||
}, | ||
"environmentProject": { | ||
"type": "string", | ||
"description": "The project to use for the environment" | ||
}, | ||
"verbose": { | ||
"type": "boolean", | ||
"description": "Print additional logs" | ||
} | ||
}, | ||
"additionalProperties": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type NpmPublishExecutorOptions = Partial<{ | ||
environmentProject: string; | ||
verbose: boolean; | ||
}>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ild-env/src/executors/setup-env/schema.ts → ...s/build-env/src/executors/setup/schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.