-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update cli, add new package types
- Loading branch information
1 parent
c37ce08
commit 2d64142
Showing
4 changed files
with
138 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,151 @@ | ||
#!/usr/bin/env node | ||
import readline from "readline"; | ||
import { promisify } from "util"; | ||
import cp from "child_process"; | ||
import path from "path"; | ||
import fs from "fs"; | ||
// cli spinners | ||
import path from "path"; | ||
import ora from "ora"; | ||
|
||
const git_repo = "https://github.com/horuslabsio/Starknet-Scaffold"; | ||
|
||
// convert libs to promises | ||
const exec = promisify(cp.exec); | ||
const rm = promisify(fs.rm); | ||
|
||
if (process.argv.length < 3) { | ||
console.log("You have to provide an app name and optionally choose if you need a basic boilerplate or a full debugger."); | ||
console.log("For example :"); | ||
console.log(" npx create-starknetkit-app my-app basic"); | ||
process.exit(1); | ||
} | ||
|
||
const projectName = process.argv[2]; | ||
const projectType = process.argv[3]; | ||
const currentPath = process.cwd(); | ||
|
||
const projectPath = path.join(currentPath, projectName); | ||
|
||
// get github repo | ||
const git_repo = "https://github.com/argentlabs/Starknet-Scaffold.git"; | ||
|
||
// create project directory | ||
if (fs.existsSync(projectPath)) { | ||
console.log(`The file ${projectName} already exist in the current directory, please give it another name.`); | ||
process.exit(1); | ||
} | ||
else { | ||
fs.mkdirSync(projectPath); | ||
} | ||
|
||
try { | ||
const gitSpinner = ora("Downloading files...").start(); | ||
// clone the repo into the project folder -> creates the new boilerplate | ||
await exec(`git clone --depth 1 ${git_repo} ${projectPath} --quiet`); | ||
gitSpinner.succeed(); | ||
|
||
let basicCleanupTasks = []; | ||
if (projectType === "basic") { | ||
const FRONTEND_BASE_PATH = "frontend/src/app"; | ||
const componentsToRemove = [ | ||
"devnet", | ||
`${FRONTEND_BASE_PATH}/burner`, | ||
`${FRONTEND_BASE_PATH}/wikipedia`, | ||
`${FRONTEND_BASE_PATH}/scaffold-deployer`, | ||
`${FRONTEND_BASE_PATH}/address-book`, | ||
`${FRONTEND_BASE_PATH}/components/Burner`, | ||
`${FRONTEND_BASE_PATH}/components/BurnerWallet`, | ||
`${FRONTEND_BASE_PATH}/components/ScaffoldDeployer`, | ||
`${FRONTEND_BASE_PATH}/components/AssetTransferModal.tsx`, | ||
`${FRONTEND_BASE_PATH}/components/ConnectionModal.tsx`, | ||
`${FRONTEND_BASE_PATH}/components/ContractExecutionModal.tsx`, | ||
]; | ||
basicCleanupTasks.push( | ||
...componentsToRemove.map((comp) => | ||
rm(path.join(projectPath, comp), { | ||
// Initialize readline interface | ||
const rl = readline.createInterface({ | ||
input: process.stdin, | ||
output: process.stdout | ||
}); | ||
|
||
// Function to ask questions in the terminal | ||
const askQuestion = (question) => { | ||
return new Promise((resolve) => rl.question(question, resolve)); | ||
}; | ||
|
||
// Main function to clone the repo and setup the package | ||
let projectPath; | ||
const installPackage = async () => { | ||
try { | ||
// Ask for package name | ||
const packageName = await askQuestion('Enter your package name: '); | ||
|
||
// Ask for package type | ||
const packageTypeChoices = ['contract_only', 'fullstack', 'debugger']; | ||
console.log('Available package types:'); | ||
packageTypeChoices.forEach((type, index) => console.log(`${index + 1}. ${type}`)); | ||
|
||
let packageType; | ||
while (!packageType) { | ||
const packageTypeChoice = await askQuestion('Select the package type (1-3): '); | ||
packageType = packageTypeChoices[parseInt(packageTypeChoice) - 1]; | ||
if (!packageType) { | ||
console.log('Invalid choice. Please select a valid package type.'); | ||
} | ||
} | ||
|
||
// create project directory | ||
const currentPath = process.cwd(); | ||
projectPath = path.join(currentPath, packageName); | ||
|
||
if (fs.existsSync(projectPath)) { | ||
console.log(`The file ${projectName} already exist in the current directory, please give it another name.`); | ||
process.exit(1); | ||
} | ||
else { | ||
fs.mkdirSync(projectPath); | ||
} | ||
|
||
// Clone the repository | ||
const gitSpinner = ora("Downloading files...").start(); | ||
// clone the repo into the project folder -> creates the new boilerplate | ||
await exec(`git clone --depth 1 ${git_repo} ${projectPath} --quiet`); | ||
gitSpinner.succeed(); | ||
|
||
let cleanupTasks = []; | ||
let excluded_files = [".git", ".github", "CONTRIBUTING.md", "bin", "burner", "website", "docs", "CNAME"]; | ||
|
||
if (packageType === "fullstack") { | ||
const FRONTEND_BASE_PATH = "frontend/src/app"; | ||
const componentsToRemove = [ | ||
`${FRONTEND_BASE_PATH}/burner`, | ||
`${FRONTEND_BASE_PATH}/wikipedia`, | ||
`${FRONTEND_BASE_PATH}/scaffold-deployer`, | ||
`${FRONTEND_BASE_PATH}/address-book`, | ||
`${FRONTEND_BASE_PATH}/components/Burner`, | ||
`${FRONTEND_BASE_PATH}/components/BurnerWallet`, | ||
`${FRONTEND_BASE_PATH}/components/ScaffoldDeployer`, | ||
`${FRONTEND_BASE_PATH}/components/AssetTransferModal.tsx`, | ||
`${FRONTEND_BASE_PATH}/components/ConnectionModal.tsx`, | ||
`${FRONTEND_BASE_PATH}/components/ContractExecutionModal.tsx`, | ||
]; | ||
cleanupTasks.push( | ||
...componentsToRemove.map((comp) => | ||
rm(path.join(projectPath, comp), { | ||
recursive: true, | ||
force: true, | ||
}), | ||
), | ||
...excluded_files.map((comp) => | ||
rm(path.join(projectPath, comp), { | ||
recursive: true, | ||
force: true, | ||
}), | ||
), | ||
); | ||
} | ||
else if (packageType == "contract_only") { | ||
let componentsToRemove = [...excluded_files, "frontend", ".editorconfig"]; | ||
cleanupTasks.push( | ||
...componentsToRemove.map((comp) => | ||
rm(path.join(projectPath, comp), { | ||
recursive: true, | ||
force: true, | ||
}), | ||
}), | ||
), | ||
); | ||
) | ||
} | ||
else { | ||
cleanupTasks.push( | ||
...excluded_files.map((comp) => | ||
rm(path.join(projectPath, comp), { | ||
recursive: true, | ||
force: true, | ||
}), | ||
), | ||
); | ||
} | ||
|
||
// remove useless files | ||
const cleanSpinner = ora("Removing useless files").start(); | ||
await Promise.all([ | ||
...cleanupTasks, | ||
]); | ||
|
||
process.chdir(projectPath); | ||
// remove the packages needed for cli | ||
await exec("npm uninstall ora cli-spinners"); | ||
cleanSpinner.succeed(); | ||
|
||
// install dependencies | ||
const npmSpinner = ora("Installing dependencies...").start(); | ||
if(packageType !== "contract_only") { | ||
await exec("npm run install --legacy-peer-deps"); | ||
} | ||
npmSpinner.succeed(); | ||
|
||
console.log("The installation is done!"); | ||
console.log("You can now run the scaffold with:"); | ||
console.log(` cd ${packageName}`); | ||
console.log(` npm run start`); | ||
|
||
} catch (err) { | ||
// clean up in case of error, so the user does not have to do it manually | ||
fs.rmSync(projectPath, { recursive: true, force: true }); | ||
console.error(`Error: ${err.message}`); | ||
} finally { | ||
rl.close(); | ||
} | ||
}; | ||
|
||
// remove useless files | ||
const cleanSpinner = ora("Removing useless files").start(); | ||
const rmGit = rm(path.join(projectPath, ".git"), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
const rmGithub = rm(path.join(projectPath, ".github"), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
const rmContributing = rm(path.join(projectPath, "CONTRIBUTING.md"), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
const rmBin = rm(path.join(projectPath, "bin"), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
const rmBurner = rm(path.join(projectPath, "burner"), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
const rmWebsite = rm(path.join(projectPath, "website"), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
const rmDocs = rm(path.join(projectPath, "docs"), { | ||
recursive: true, | ||
force: true, | ||
}); | ||
await Promise.all([ | ||
rmGit, | ||
rmBin, | ||
rmGithub, | ||
rmContributing, | ||
rmBurner, | ||
rmWebsite, | ||
rmDocs, | ||
...basicCleanupTasks, | ||
]); | ||
|
||
process.chdir(projectPath); | ||
// remove the packages needed for cli | ||
await exec("npm uninstall ora cli-spinners"); | ||
cleanSpinner.succeed(); | ||
|
||
// install dependencies | ||
const npmSpinner = ora("Installing dependencies...").start(); | ||
await exec("npm run install"); | ||
npmSpinner.succeed(); | ||
|
||
console.log("The installation is done!"); | ||
console.log("You can now run the scaffold with:"); | ||
console.log(` cd ${projectName}`); | ||
console.log(` npm run start`); | ||
} catch (error) { | ||
// clean up in case of error, so the user does not have to do it manually | ||
fs.rmSync(projectPath, { recursive: true, force: true }); | ||
console.log(error); | ||
} | ||
installPackage(); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters