Skip to content

Commit 2720b01

Browse files
committed
add progress
1 parent de848cd commit 2720b01

File tree

14 files changed

+150
-19
lines changed

14 files changed

+150
-19
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[package]
2+
name = "project_name"
3+
version = "1.0.0"
4+
edition = "2021"
5+
license = "MIT"
6+
7+
[lib]
8+
crate-type = ["cdylib"]
9+
10+
[dependencies.napi]
11+
version = "3.1"
12+
default-features = false
13+
# see https://nodejs.org/api/n-api.html#node-api-version-matrix
14+
features = ["napi3"]
15+
16+
[dependencies.napi-derive]
17+
version = "3.1"
18+
features = ["type-def"]
19+
20+
[build-dependencies]
21+
napi-build = "2"
22+
23+
[profile.release]
24+
lto = true
25+
codegen-units = 1
26+
strip = "symbols"
27+
opt-level = "z"

apps/test-app/lib_in_rust/build.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
napi_build::setup();
3+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "project_name",
3+
"private": true,
4+
"version": "0.1.0",
5+
"main": "dist/project_name.js",
6+
"types": "dist/project_name.d.ts",
7+
"scripts": {
8+
"build": "ferric build",
9+
"build:release": "npm run build -- --configuration release"
10+
},
11+
"devDependencies": {
12+
"ferric-cli": "latest"
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use napi_derive::napi;
2+
3+
#[napi]
4+
pub fn sum(a: i32, b: i32) -> i32 {
5+
a + b
6+
}

apps/test-app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
"@types/mocha": "^10.0.10",
3434
"@types/react": "^19.0.0",
3535
"concurrently": "^9.1.2",
36-
"ferric-cli": "*",
36+
"ferric-cli": "workspace:*",
3737
"mocha": "^11.6.0",
3838
"mocha-remote-cli": "^1.13.2",
3939
"mocha-remote-react-native": "^1.13.2",
4040
"react": "19.0.0",
4141
"react-native": "0.79.5",
42-
"react-native-node-addon-examples": "*",
42+
"@react-native-node-api/node-addon-examples": "workspace:*",
4343
"react-native-node-api": "*",
4444
"react-native-test-app": "^4.3.3"
4545
}

packages/ferric/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
},
1515
"scripts": {
1616
"start": "tsx src/run.ts",
17-
"test": "tsx --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout src/**/*.test.ts"
17+
"build": "tsc --build --verbose && pnpm copy-templates",
18+
"test": "tsx --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout src/**/*.test.ts",
19+
"copy-templates": "copyfiles -u 1 src/templates/**/* dist"
1820
},
1921
"dependencies": {
2022
"@commander-js/extra-typings": "^13.1.0",
@@ -24,5 +26,8 @@
2426
"commander": "^13.1.0",
2527
"ora": "^8.2.0",
2628
"react-native-node-api": "0.3.2"
29+
},
30+
"devDependencies": {
31+
"copyfiles": "^2.4.1"
2732
}
2833
}

packages/ferric/src/build.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import assert from "node:assert/strict";
22
import { describe, it } from "node:test";
33

44
describe("build", () => {
5-
it("", () => {
6-
assert.equal(1, 1);
7-
});
5+
it("", () => {
6+
// Needs init, to generate project and then build
7+
assert.equal(1, 1);
8+
});
89
});

packages/ferric/src/init.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
22
import { describe, it } from "node:test";
33

44
describe("init", () => {
5-
it("", () => {
6-
assert.equal(1, 1);
7-
});
5+
it("", () => {
6+
assert.equal(1, 1);
7+
});
88
});

packages/ferric/src/init.ts

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,75 @@
11
import { Command } from "@commander-js/extra-typings";
2+
import fs from "node:fs";
3+
import fsPromise from "node:fs/promises";
4+
import path from "node:path";
5+
import { oraPromise } from "ora";
6+
import { prettyPath } from "react-native-node-api";
27

38
export const initCommand = new Command("init")
4-
.description("Generate the project scaffold")
5-
.argument("<name>", "Type the project name")
6-
.action(() => {
9+
.description("Generate the project scaffold")
10+
.argument("<name>", "Type the project name")
11+
.action(async (str) => {
12+
const projectName = str && str.length > 0 ? str : "ferric_project";
13+
const generatePath = path.join(process.cwd(), projectName);
14+
await oraPromise(
15+
generateProject({ outputPath: generatePath, projectName }),
16+
{
17+
text: "Generating project",
18+
successText: `Generated project ${prettyPath(generatePath)}`,
19+
failText: (error) => `Failed to generate the project: ${error.message}`,
20+
},
21+
);
22+
});
723

8-
})
24+
async function replaceStrInFile(
25+
filePath: string,
26+
oldStr: string,
27+
newStr: string,
28+
) {
29+
const content = await fsPromise.readFile(filePath, "utf8");
30+
const updatedContent = content.replace(`/${oldStr}/gi`, newStr);
31+
await fsPromise.writeFile(filePath, updatedContent, "utf8");
32+
}
33+
34+
function createFolder(path: string) {
35+
if (!fs.existsSync(path)) {
36+
fs.mkdirSync(path);
37+
}
38+
}
39+
40+
async function copyAllTemplateFiles(outputFilePath: string) {
41+
const templateDir = path.join(import.meta.dirname, "templates");
42+
await fsPromise.cp(templateDir, outputFilePath, {
43+
recursive: true,
44+
});
45+
await fsPromise.rename(
46+
`${outputFilePath}/lib.rs`,
47+
`${outputFilePath}/src/lib.rs`,
48+
);
49+
await fsPromise.rename(
50+
`${outputFilePath}/gitignore`,
51+
`${outputFilePath}/.gitignore`,
52+
);
53+
}
54+
55+
async function generateProject({
56+
outputPath,
57+
projectName,
58+
}: {
59+
outputPath: string;
60+
projectName: string;
61+
}) {
62+
createFolder(outputPath);
63+
createFolder(`${outputPath}/src`);
64+
await copyAllTemplateFiles(outputPath);
65+
await replaceStrInFile(
66+
`${outputPath}/package.json`,
67+
"project_name",
68+
projectName,
69+
);
70+
await replaceStrInFile(
71+
`${outputPath}/Cargo.toml`,
72+
"project_name",
73+
projectName,
74+
);
75+
}

0 commit comments

Comments
 (0)