Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update setup-project.js #412

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 47 additions & 34 deletions cli/setup-project.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,28 @@ const { consola } = require('consola');
const fs = require('fs-extra');
const path = require('path');

// Execute shell command within a project directory
const execInProjectDir = async (projectName, command) => {
const projectPath = path.join(process.cwd(), projectName);
await execShellCommand(`cd ${projectPath} && ${command}`);
};

// Initialize Git repository
const initGit = async (projectName) => {
await execShellCommand(`cd ${projectName} && git init && cd ..`);
await execInProjectDir(projectName, 'git init');
};

// Install dependencies using pnpm
const installDeps = async (projectName) => {
await runCommand(`cd ${projectName} && pnpm install`, {
loading: 'Installing project dependencies',
const projectPath = path.join(process.cwd(), projectName);
await runCommand(`cd ${projectPath} && pnpm install`, {
loading: 'Installing project dependencies',
success: 'Dependencies installed',
error: 'Failed to install dependencies, Make sure you have pnpm installed',
error: 'Failed to install dependencies. Make sure you have pnpm installed.',
});
};

// remove unnecessary files, such us .git, ios, android, docs, cli, LICENSE
// Remove unnecessary files from the project directory
const removeFiles = async (projectName) => {
const FILES_TO_REMOVE = [
'.git',
Expand All @@ -27,59 +36,63 @@ const removeFiles = async (projectName) => {
'LICENSE',
];

FILES_TO_REMOVE.forEach((file) => {
fs.removeSync(path.join(process.cwd(), `${projectName}/${file}`));
});
const projectPath = path.join(process.cwd(), projectName);
for (const file of FILES_TO_REMOVE) {
const filePath = path.join(projectPath, file);
if (await fs.pathExists(filePath)) {
await fs.remove(filePath);
}
}
};

// Update package.json infos, name and set version to 0.0.1 + add initial version to osMetadata
// Update package.json details
const updatePackageInfos = async (projectName) => {
const packageJsonPath = path.join(
process.cwd(),
`${projectName}/package.json`
);
const packageJson = fs.readJsonSync(packageJsonPath);
const packageJsonPath = path.join(process.cwd(), `${projectName}/package.json`);
const packageJson = await fs.readJson(packageJsonPath);

packageJson.osMetadata = { initVersion: packageJson.version };
packageJson.version = '0.0.1';
packageJson.name = projectName?.toLowerCase();
packageJson.name = projectName.toLowerCase();
packageJson.repository = {
type: 'git',
url: 'git+https://github.com/user/repo-name.git',
};
fs.writeJsonSync(packageJsonPath, packageJson, { spaces: 2 });

await fs.writeJson(packageJsonPath, packageJson, { spaces: 2 });
};

// Update configuration files with project-specific values
const updateProjectConfig = async (projectName) => {
const configPath = path.join(process.cwd(), `${projectName}/env.js`);
const contents = fs.readFileSync(configPath, {
encoding: 'utf-8',
});
const contents = await fs.readFile(configPath, 'utf-8');

const replaced = contents
.replace(/ObytesApp/gi, projectName)
.replace(/com.obytes/gi, `com.${projectName.toLowerCase()}`)
.replace(/obytes/gi, 'expo-owner');

fs.writeFileSync(configPath, replaced, { spaces: 2 });
const readmeFilePath = path.join(
process.cwd(),
`${projectName}/README-project.md`
);
fs.renameSync(
readmeFilePath,
path.join(process.cwd(), `${projectName}/README.md`)
);
await fs.writeFile(configPath, replaced);

const readmeFilePath = path.join(process.cwd(), `${projectName}/README-project.md`);
const newReadmePath = path.join(process.cwd(), `${projectName}/README.md`);

if (await fs.pathExists(readmeFilePath)) {
await fs.rename(readmeFilePath, newReadmePath);
}
};

// Main function to set up the project
const setupProject = async (projectName) => {
consola.start(`Clean up and setup your project 🧹`);
consola.start(`Cleaning up and setting up your project 🧹`);

try {
removeFiles(projectName);
await removeFiles(projectName);
await initGit(projectName);
updatePackageInfos(projectName);
updateProjectConfig(projectName);
consola.success(`Clean up and setup your project 🧹`);
await updatePackageInfos(projectName);
await updateProjectConfig(projectName);
consola.success(`Project setup completed successfully 🧹`);
} catch (error) {
consola.error(`Failed to clean up project folder`, error);
consola.error(`Failed to set up the project:`, error);
process.exit(1);
}
};
Expand Down