-
Notifications
You must be signed in to change notification settings - Fork 17
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
1 parent
fdef71a
commit 2fbf4ef
Showing
6 changed files
with
140 additions
and
3 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { ExecutorContext, joinPathFragments, output } from '@nx/devkit'; | ||
import { execSync } from 'node:child_process'; | ||
import { readFileSync } from 'node:fs'; | ||
import { relative } from 'node:path'; | ||
import { env as appendLocalEnv } from 'npm-run-path'; | ||
import { parseCargoToml } from '../../utils/toml'; | ||
import { PublishExecutorSchema } from './schema'; | ||
import chalk = require('chalk'); | ||
|
||
const LARGE_BUFFER = 1024 * 1000000; | ||
|
||
function processEnv(color: boolean) { | ||
const env = { | ||
...process.env, | ||
...appendLocalEnv(), | ||
}; | ||
|
||
if (color) { | ||
env.FORCE_COLOR = `${color}`; | ||
} | ||
return env; | ||
} | ||
|
||
export default async function runExecutor( | ||
options: PublishExecutorSchema, | ||
context: ExecutorContext | ||
) { | ||
/** | ||
* We need to check both the env var and the option because the executor may have been triggered | ||
* indirectly via dependsOn, in which case the env var will be set, but the option will not. | ||
*/ | ||
const isDryRun = process.env.NX_DRY_RUN === 'true' || options.dryRun || false; | ||
|
||
const projectConfig = | ||
context.projectsConfigurations!.projects[context.projectName!]!; | ||
|
||
const packageRoot = joinPathFragments( | ||
context.root, | ||
options.packageRoot ?? projectConfig.root | ||
); | ||
const workspaceRelativePackageRoot = relative(context.root, packageRoot); | ||
|
||
const cargoTomlPath = joinPathFragments(packageRoot, 'Cargo.toml'); | ||
const cargoTomlContents = readFileSync(cargoTomlPath, 'utf-8'); | ||
const cargoToml = parseCargoToml(cargoTomlContents); | ||
const crateName = cargoToml.package.name; | ||
|
||
const cargoPublishCommandSegments = [ | ||
`cargo publish --allow-dirty -p ${crateName} --target-dir ${workspaceRelativePackageRoot}/target`, | ||
]; | ||
|
||
if (isDryRun) { | ||
cargoPublishCommandSegments.push(`--dry-run`); | ||
} | ||
|
||
try { | ||
const command = cargoPublishCommandSegments.join(' '); | ||
output.logSingleLine(`Running "${command}"...`); | ||
|
||
execSync(command, { | ||
maxBuffer: LARGE_BUFFER, | ||
env: processEnv(true), | ||
cwd: packageRoot, | ||
stdio: 'inherit', | ||
}); | ||
|
||
console.log(''); | ||
|
||
if (isDryRun) { | ||
console.log( | ||
`Would publish to https://crates.io, but ${chalk.keyword('orange')( | ||
'[dry-run]' | ||
)} was set` | ||
); | ||
} else { | ||
console.log(`Published to https://crates.io`); | ||
} | ||
|
||
return { | ||
success: true, | ||
}; | ||
} catch (err: any) { | ||
return { | ||
success: false, | ||
}; | ||
} | ||
} |
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 interface PublishExecutorSchema { | ||
packageRoot?: string; | ||
dryRun?: 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"$schema": "https://json-schema.org/schema", | ||
"version": 2, | ||
"title": "Implementation details of `nx release publish`", | ||
"description": "DO NOT INVOKE DIRECTLY WITH `nx run`. Use `nx release publish` instead.", | ||
"type": "object", | ||
"properties": { | ||
"packageRoot": { | ||
"type": "string", | ||
"description": "The root directory of the directory (containing a manifest file at its root) to publish. Defaults to the project root." | ||
}, | ||
"dryRun": { | ||
"type": "boolean", | ||
"description": "Whether to run the command without actually publishing the package to the registry." | ||
} | ||
}, | ||
"required": [] | ||
} |
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