create-react-native-mono-template
Version:
React native mono template init
148 lines (131 loc) • 4.68 kB
JavaScript
const { execShellCommand, runCommand } = require("./utils.js");
const { consola } = require("consola");
const fs = require("fs-extra");
const path = require("path");
const initGit = async (projectName) => {
await execShellCommand(`cd ${projectName} && git init && cd ..`);
};
const installDeps = async (projectName) => {
await runCommand(`cd ${projectName} && yarn`, {
loading: "Installing project dependencies",
success: "Dependencies installed",
error: "Failed to install dependencies, Make sure you have yarn installed",
});
};
// remove unnecessary files, such us .git, ios, android, docs, cli, LICENSE
const removeFiles = async (projectName) => {
const FILES_TO_REMOVE = [
".git",
"README.md",
"ios",
"android",
"docs",
"cli",
"LICENSE",
];
FILES_TO_REMOVE.forEach((file) => {
fs.removeSync(path.join(process.cwd(), `${projectName}/${file}`));
});
};
// Update package.json infos, name and set version to 0.0.1 + add initial version to osMetadata
const updatePackageInfos = async (projectName) => {
const packageJsonPath = path.join(
process.cwd(),
`${projectName}/package.json`
);
const packageJson = fs.readJsonSync(packageJsonPath);
packageJson.osMetadata = { initVersion: packageJson.version };
packageJson.version = "0.0.1";
packageJson.name = projectName?.toLowerCase();
packageJson.scripts[
"e2e-test"
] = `maestro test .maestro/ -e APP_ID=com.${projectName.toLowerCase()}`;
packageJson.repository = {
type: "git",
url: "git+https://github.com/user/repo-name.git",
};
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });
};
// Update app.config.js with project specific values
const updateAppConfig = async (projectName) => {
const appConfigPath = path.join(
process.cwd(),
`${projectName}/app.config.js`
);
if (fs.existsSync(appConfigPath)) {
let contents = fs.readFileSync(appConfigPath, {
encoding: "utf-8",
});
let packageID = `com.${projectName.toLowerCase()}`;
// Update name, slug, scheme, and bundle identifiers
contents = contents
// Update name to projectName
.replace(/name: "Mono template"/gi, `name: "${projectName}"`)
// Update slug to com-projectName.toLowerCase()
.replace(
/slug: "mono-template"/gi,
`slug: "com-${projectName.toLowerCase()}"`
)
// Update scheme to projectName.toLowerCase()
.replace(/scheme: "mono.template"/gi, `scheme: "${packageID}"`)
// Update CFBundleSpokenName to projectName
.replace(
/CFBundleSpokenName: "React Native Mono Template"/gi,
`CFBundleSpokenName: "${projectName}"`
)
// Update iOS bundleIdentifier
.replace(
/bundleIdentifier: "com.reactnative.mono.template"/gi,
`bundleIdentifier: "${packageID}"`
)
// Update Android package
.replace(
/package: "com.reactnative.mono.template"/gi,
`package: "${packageID}"`
);
fs.writeFileSync(appConfigPath, contents, { encoding: "utf-8" });
consola.success(`Updated app.config.js with project specific values`);
} else {
consola.warn(`app.config.js not found, skipping configuration`);
}
};
const updateProjectEnv = async (projectName) => {
// Danh sách các file env cần cập nhật
const envFiles = [".env.development", ".env.staging", ".env.production"];
// Lặp qua từng file env và cập nhật
for (const envFile of envFiles) {
const configPath = path.join(process.cwd(), `${projectName}/${envFile}`);
// Kiểm tra xem file có tồn tại không
if (fs.existsSync(configPath)) {
const contents = fs.readFileSync(configPath, {
encoding: "utf-8",
});
const replaced = contents.replace(
/com.reactnative.mono.template/gi,
`com.${projectName.toLowerCase()}`
);
fs.writeFileSync(configPath, replaced, { spaces: 2 });
consola.success(`Updated ${envFile} with project specific values`);
} else {
consola.warn(`${envFile} not found, skipping configuration`);
}
}
};
const setupProject = async (projectName) => {
consola.start(`Clean up and setup your project 🧹`);
try {
removeFiles(projectName);
await initGit(projectName);
updatePackageInfos(projectName);
updateProjectEnv(projectName);
await updateAppConfig(projectName); // Thêm hàm mới để cập nhật app.config.js
consola.success(`Clean up and setup your project 🧹`);
} catch (error) {
consola.error(`Failed to clean up project folder`, error);
process.exit(1);
}
};
module.exports = {
setupProject,
installDeps,
};