Skip to content

Commit

Permalink
Merge pull request #1047 from neon-bindings/create-neon-refactor
Browse files Browse the repository at this point in the history
refactor(create-neon): control flow cleanup
  • Loading branch information
dherman authored Jun 3, 2024
2 parents a07025c + 3bd98e0 commit f2f19d4
Show file tree
Hide file tree
Showing 22 changed files with 468 additions and 364 deletions.
2 changes: 1 addition & 1 deletion pkgs/create-neon/data/templates/.gitignore.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ target
index.node
**/node_modules
**/.DS_Store
npm-debug.log*{{#eq packageSpec.library.lang compare="ts"}}
npm-debug.log*{{#eq options.library.lang compare="ts"}}
lib
{{/eq}}
cargo.log
Expand Down
16 changes: 8 additions & 8 deletions pkgs/create-neon/data/templates/Cargo.toml.hbs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
[package]
name = "{{package.name}}"
version = "{{package.version}}"
{{#if package.description}}
description = {{package.quotedDescription}}
name = {{crate.escaped.name}}
version = {{crate.escaped.version}}
{{#if crate.description}}
description = {{crate.escaped.description}}
{{/if}}
{{#if package.author}}
authors = [{{package.quotedAuthor}}]
{{#if crate.author}}
authors = [{{crate.escaped.author}}]
{{/if}}
{{#if package.license}}
license = "{{package.license}}"
{{#if crate.license}}
license = {{crate.escaped.license}}
{{/if}}
edition = "2021"
exclude = ["index.node"]
Expand Down
14 changes: 7 additions & 7 deletions pkgs/create-neon/data/templates/README.md.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This command uses the [@neon-rs/cli](https://www.npmjs.com/package/@neon-rs/cli)

After building {{package.name}}, you can explore its exports at the Node console:

{{#if packageSpec.library}}
{{#if options.library}}
```sh
$ npm i
$ npm run build
Expand All @@ -44,7 +44,7 @@ $ node

In the project directory, you can run:

{{#unless packageSpec.library}}
{{#unless options.library}}
#### `npm install`

Installs the project, including running `npm run build`.
Expand All @@ -68,7 +68,7 @@ Similar to `npm run build` but generates a debug build with `cargo`.

Similar to `npm run build` but uses [cross-rs](https://github.com/cross-rs/cross) to cross-compile for another platform. Use the [`CARGO_BUILD_TARGET`](https://doc.rust-lang.org/cargo/reference/config.html#buildtarget) environment variable to select the build target.

{{#eq packageSpec.library.ci.type compare="github"}}
{{#eq options.library.ci.type compare="github"}}
#### `npm run release`

Initiate a full build and publication of a new patch release of this library via GitHub Actions.
Expand All @@ -90,9 +90,9 @@ The directory structure of this project is:
{{package.name}}/
├── Cargo.toml
├── README.md
{{#if packageSpec.library}}
{{#if options.library}}
├── lib/
{{#eq packageSpec.library.lang compare="ts"}}
{{#eq options.library.lang compare="ts"}}
├── src/
| ├── index.mts
| └── index.cts
Expand All @@ -115,8 +115,8 @@ The directory structure of this project is:
|----------------|------------------------------------------------------------------------------------------------------------------------------------------|
| `Cargo.toml` | The Cargo [manifest file](https://doc.rust-lang.org/cargo/reference/manifest.html), which informs the `cargo` command. |
| `README.md` | This file. |
{{#if packageSpec.library}}
{{#eq packageSpec.library.lang compare="ts"}}
{{#if options.library}}
{{#eq options.library.lang compare="ts"}}
| `lib/` | The directory containing the generated output from [tsc](https://typescriptlang.org). |
| `src/` | The directory containing the TypeScript source files. |
| `index.mts` | Entry point for when this library is loaded via [ESM `import`](https://nodejs.org/api/esm.html#modules-ecmascript-modules) syntax. |
Expand Down
2 changes: 1 addition & 1 deletion pkgs/create-neon/data/templates/Workspace.toml.hbs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[workspace]
members = ["crates/{{package.name}}"]
members = ["crates/{{crate.name}}"]
resolver = "2"

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

{
"name": "{{packageSpec.name}}",
"version": "{{packageSpec.version}}",
"name": "{{options.name}}",
"version": "{{options.version}}",
"main": "index.node",
"scripts": {},
"devDependencies": {
"@neon-rs/cli": "{{versions.neonCLI}}"{{#eq packageSpec.library.lang compare="ts"}},
"@neon-rs/cli": "{{versions.neonCLI}}"{{#eq options.library.lang compare="ts"}},
"@tsconfig/node{{versions.tsconfigNode.major}}": "^{{versions.tsconfigNode.semver}}",
"@types/node": "^{{versions.typesNode}}",
"typescript": "^{{versions.typescript}}"{{/eq}}
Expand Down
14 changes: 7 additions & 7 deletions pkgs/create-neon/data/templates/manifest/base/library.json.hbs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

{
"name": "{{packageSpec.name}}",
"version": "{{packageSpec.version}}",
{{#eq packageSpec.library.module compare="esm"}}
"name": "{{options.name}}",
"version": "{{options.version}}",
{{#eq options.library.module compare="esm"}}
"exports": {
".": {
"import": {
Expand All @@ -24,16 +24,16 @@
"scripts": {},
"neon": {
"type": "library",
{{#eq packageSpec.library.cache.type compare="npm"}}
{{#if packageSpec.library.cache.org}}
"org": "{{packageSpec.library.cache.org}}",
{{#eq options.library.cache.type compare="npm"}}
{{#if options.library.cache.org}}
"org": "{{options.library.cache.org}}",
{{/if}}
{{/eq}}
"platforms": {},
"load": "./src/load.cts"
},
"devDependencies": {
"@neon-rs/cli": "^{{versions.neonCLI}}"{{#eq packageSpec.library.lang compare="ts"}},
"@neon-rs/cli": "^{{versions.neonCLI}}"{{#eq options.library.lang compare="ts"}},
"@tsconfig/node{{versions.tsconfigNode.major}}": "^{{versions.tsconfigNode.semver}}",
"@types/node": "^{{versions.typesNode}}",
"typescript": "^{{versions.typescript}}"{{/eq}}
Expand Down
12 changes: 0 additions & 12 deletions pkgs/create-neon/data/templates/manifest/scripts.json.hbs

This file was deleted.

2 changes: 1 addition & 1 deletion pkgs/create-neon/data/templates/tsconfig.json.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
{{#eq packageSpec.library.lang compare="ts"}}
{{#eq options.library.lang compare="ts"}}
"extends": "@tsconfig/node{{versions.tsconfigNode.major}}/tsconfig.json",
"compilerOptions": {
"module": "node{{versions.tsconfigNode.module}}",
Expand Down
32 changes: 9 additions & 23 deletions pkgs/create-neon/src/bin/create-neon.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
#!/usr/bin/env node

import * as path from "path";
import commandLineArgs from "command-line-args";
import { printErrorWithUsage } from "../print.js";
import { createNeon } from "../index.js";
import { Cache } from "../cache.js";
import { NPM } from "../cache/npm.js";
import { CI } from "../ci.js";
import { GitHub } from "../ci/github.js";
import { Lang, ModuleType } from "../package.js";
import { Lang, ModuleType } from "../create/creator.js";
import {
NodePlatform,
PlatformPreset,
assertIsPlatformPreset,
isNodePlatform,
isPlatformPreset,
} from "@neon-rs/manifest/platform";

const JS_TEMPLATES: Record<string, string> = {
".gitignore.hbs": ".gitignore",
"Cargo.toml.hbs": "Cargo.toml",
"README.md.hbs": "README.md",
"lib.rs.hbs": path.join("src", "lib.rs"),
};

function tsTemplates(pkg: string): Record<string, string> {
return {
".gitignore.hbs": ".gitignore",
"Cargo.toml.hbs": path.join("crates", pkg, "Cargo.toml"),
"Workspace.toml.hbs": "Cargo.toml",
"README.md.hbs": "README.md",
"lib.rs.hbs": path.join("crates", pkg, "src", "lib.rs"),
};
}

const OPTIONS = [
{ name: "app", type: Boolean, defaultValue: false },
{ name: "lib", type: Boolean, defaultValue: false },
Expand Down Expand Up @@ -67,8 +48,9 @@ try {
process.env["npm_configure_yes"] = "true";
}

createNeon(pkg, {
templates: opts.lib ? tsTemplates(pkg) : JS_TEMPLATES,
createNeon({
name: pkg,
version: "0.1.0",
library: opts.lib
? {
lang: Lang.TS,
Expand All @@ -78,7 +60,11 @@ try {
platforms,
}
: null,
app: opts.app ? true : null,
app: opts.app ? true : opts.lib ? false : null,
// Even if the user specifies this with a flag (e.g. `npm init -y neon`),
// `npm init` sets this env var to 'true' before invoking create-neon.
// So this is the most general way to check this configuration option.
interactive: process.env["npm_configure_yes"] !== "true",
});
} catch (e) {
printErrorWithUsage(e);
Expand Down
1 change: 1 addition & 0 deletions pkgs/create-neon/src/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export interface CI {
readonly type: string;
templates(): Record<string, string>;
setup(): void;
scripts(): Record<string, string>;
}
7 changes: 7 additions & 0 deletions pkgs/create-neon/src/ci/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,11 @@ export class GitHub implements CI {
setup(): void {
handlebars.registerHelper("$", githubDelegate);
}

scripts(): Record<string, string> {
return {
release: "gh workflow run release.yml -f dryrun=false -f version=patch",
dryrun: "gh workflow run publish.yml -f dryrun=true",
};
}
}
20 changes: 20 additions & 0 deletions pkgs/create-neon/src/create/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Creator, ProjectOptions } from "./creator.js";

export class AppCreator extends Creator {
constructor(options: ProjectOptions) {
super(options);
}

scripts(): Record<string, string> {
return {
test: "cargo test",
"cargo-build": "cargo build --message-format=json > cargo.log",
"cross-build": "cross build --message-format=json > cross.log",
"postcargo-build": "neon dist < cargo.log",
"postcross-build": "neon dist -m /target < cross.log",
debug: "npm run cargo-build --",
build: "npm run cargo-build -- --release",
cross: "npm run cross-build -- --release",
};
}
}
Loading

0 comments on commit f2f19d4

Please sign in to comment.