Skip to content

Commit 1ac1df2

Browse files
committed
fixed replacing
1 parent 2720b01 commit 1ac1df2

File tree

5 files changed

+75
-57
lines changed

5 files changed

+75
-57
lines changed

apps/test-app/lib_in_rust/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "project_name"
2+
name = "lib_in_rust"
33
version = "1.0.0"
44
edition = "2021"
55
license = "MIT"

apps/test-app/lib_in_rust/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
2-
"name": "project_name",
2+
"name": "lib_in_rust",
33
"private": true,
44
"version": "0.1.0",
5-
"main": "dist/project_name.js",
6-
"types": "dist/project_name.d.ts",
5+
"main": "dist/lib_in_rust.js",
6+
"types": "dist/lib_in_rust.d.ts",
77
"scripts": {
88
"build": "ferric build",
99
"build:release": "npm run build -- --configuration release"

packages/ferric/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
11
# `ferric`
22

33
A wrapper around Cargo making it easier to produce prebuilt binaries targeting iOS and Android matching the [the prebuilt binary specification](https://github.com/callstackincubator/react-native-node-api/blob/main/docs/PREBUILDS.md) as well as [napi.rs](https://napi.rs/) to generate bindings from annotated Rust code.
4+
5+
### Project Structure
6+
7+
After generating the project, a folder would be created with the following file structure for the lib.
8+
9+
```markdown
10+
lib_name
11+
│ Cargo.toml
12+
│ build.rs
13+
│ .gitignore
14+
└───src
15+
│ │ lib.rs
16+
└───dist (after build)
17+
│ │ index.js
18+
│ │ index.d.ts
19+
│ │ native_android_folder
20+
│ │ native_ios_folder
21+
```

packages/ferric/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
},
1515
"scripts": {
1616
"start": "tsx src/run.ts",
17-
"build": "tsc --build --verbose && pnpm copy-templates",
17+
"build": "tsc --build && pnpm copy-templates",
1818
"test": "tsx --test --test-reporter=@reporters/github --test-reporter-destination=stdout --test-reporter=spec --test-reporter-destination=stdout src/**/*.test.ts",
1919
"copy-templates": "copyfiles -u 1 src/templates/**/* dist"
2020
},

packages/ferric/src/init.ts

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,70 +6,70 @@ import { oraPromise } from "ora";
66
import { prettyPath } from "react-native-node-api";
77

88
export const initCommand = new Command("init")
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-
});
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+
});
2323

2424
async function replaceStrInFile(
25-
filePath: string,
26-
oldStr: string,
27-
newStr: string,
25+
filePath: string,
26+
oldStr: string,
27+
newStr: string,
2828
) {
29-
const content = await fsPromise.readFile(filePath, "utf8");
30-
const updatedContent = content.replace(`/${oldStr}/gi`, newStr);
31-
await fsPromise.writeFile(filePath, updatedContent, "utf8");
29+
const content = await fsPromise.readFile(filePath, "utf8");
30+
const updatedContent = content.replaceAll(oldStr, newStr);
31+
await fsPromise.writeFile(filePath, updatedContent, "utf8");
3232
}
3333

3434
function createFolder(path: string) {
35-
if (!fs.existsSync(path)) {
36-
fs.mkdirSync(path);
37-
}
35+
if (!fs.existsSync(path)) {
36+
fs.mkdirSync(path);
37+
}
3838
}
3939

4040
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-
);
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+
);
5353
}
5454

5555
async function generateProject({
56-
outputPath,
57-
projectName,
56+
outputPath,
57+
projectName,
5858
}: {
59-
outputPath: string;
60-
projectName: string;
59+
outputPath: string;
60+
projectName: string;
6161
}) {
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-
);
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+
);
7575
}

0 commit comments

Comments
 (0)