-
-
Notifications
You must be signed in to change notification settings - Fork 129
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
feature: better frameworks like cli #115
Merged
Merged
Changes from all commits
Commits
Show all changes
44 commits
Select commit
Hold shift + click to select a range
a78b815
better cli
vasucp1207 abc2096
playground
vasucp1207 04cdc5c
build project
vasucp1207 c81c506
build project
vasucp1207 78551d2
remove esbuild templates
vasucp1207 51df8a4
remove esbuild templates
vasucp1207 1bdaba8
update test
vasucp1207 1d36e1e
trying building the package
vasucp1207 c387dcd
trying building the package
vasucp1207 82cd7ac
move files to scr/ folder
vasucp1207 5459f1b
move files to scr/ folder
vasucp1207 0499830
deepMerge fix
vasucp1207 2884f69
adding file to package.json
vasucp1207 26cb8db
adding template/ to package
vasucp1207 a0e0890
build change
vasucp1207 58b4e46
remove emptyDir and renderDir
vasucp1207 91da4bf
replace with cross-spawn
vasucp1207 ac5536a
replace with cross-spawn
vasucp1207 c375ed7
remove cross-spawn
vasucp1207 f027c6c
remove cross-spawn
vasucp1207 e01c429
Update contrib/create-waku/src/cli.ts
vasucp1207 c8e8b96
Update contrib/create-waku/src/cli.ts
vasucp1207 5ffbd5e
Update contrib/create-waku/src/cli.ts
vasucp1207 0713dc4
relative to the script location
vasucp1207 045d405
remove test file
vasucp1207 6cffde3
Update contrib/create-waku/src/cli.ts
vasucp1207 2a98e6a
Update contrib/create-waku/src/cli.ts
vasucp1207 2fc635b
refactor
vasucp1207 5fa654c
packageJson write
vasucp1207 00bc21f
packageJson write
vasucp1207 ef34f61
remove render
vasucp1207 6b1ede3
remove render
vasucp1207 c65f424
rebase
vasucp1207 9dff783
new monorepo structure
vasucp1207 b7f030c
new monorepo structure
vasucp1207 f025623
update dir structure
vasucp1207 6b990b3
Update package.json
vasucp1207 472d830
update pnpm yaml
vasucp1207 31792f0
Update packages/create-waku/package.json
dai-shi 2a52aac
types for fs-extra
vasucp1207 81a54d0
Update types.d.ts
vasucp1207 016f005
fix prettier issue
vasucp1207 47e71fa
Update .gitignore
vasucp1207 4c549d9
Update packages/create-waku/src/cli.ts
dai-shi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
*.swp | ||
node_modules | ||
dist | ||
/packages/create-waku/template |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,169 @@ | ||
#!/usr/bin/env node | ||
|
||
import fs from "node:fs"; | ||
import path from "node:path"; | ||
import prompts from "prompts"; | ||
import { red, green, bold } from "kolorist"; | ||
import fse from "fs-extra/esm"; | ||
|
||
function isValidPackageName(projectName: string) { | ||
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test( | ||
projectName, | ||
); | ||
} | ||
|
||
function toValidPackageName(projectName: string) { | ||
return projectName | ||
.trim() | ||
.toLowerCase() | ||
.replace(/\s+/g, "-") | ||
.replace(/^[._]/, "") | ||
.replace(/[^a-z0-9-~]+/g, "-"); | ||
} | ||
|
||
// if the dir is empty or not exist | ||
function canSafelyOverwrite(dir: string) { | ||
return !fs.existsSync(dir) || fs.readdirSync(dir).length === 0; | ||
} | ||
|
||
async function init() { | ||
let targetDir = ""; | ||
const defaultProjectName = "waku-project"; | ||
|
||
const CHOICES = fs.readdirSync("template"); | ||
let result: { | ||
packageName: string; | ||
shouldOverwrite: string; | ||
chooseProject: string; | ||
}; | ||
|
||
try { | ||
result = await prompts( | ||
[ | ||
{ | ||
name: "projectName", | ||
type: "text", | ||
message: "Project Name", | ||
initial: defaultProjectName, | ||
onState: (state: any) => | ||
(targetDir = String(state.value).trim() || defaultProjectName), | ||
}, | ||
{ | ||
name: "shouldOverwrite", | ||
type: () => (canSafelyOverwrite(targetDir) ? null : "confirm"), | ||
message: `${targetDir} is not empty. Remove existing files and continue?`, | ||
}, | ||
{ | ||
name: "overwriteChecker", | ||
type: (values: any) => { | ||
if (values === false) { | ||
throw new Error(red("✖") + " Operation cancelled"); | ||
} | ||
return null; | ||
}, | ||
}, | ||
{ | ||
name: "packageName", | ||
type: () => (isValidPackageName(targetDir) ? null : "text"), | ||
message: "Package name", | ||
initial: () => toValidPackageName(targetDir), | ||
validate: (dir: string) => | ||
isValidPackageName(dir) || "Invalid package.json name", | ||
}, | ||
{ | ||
name: "chooseProject", | ||
type: "select", | ||
message: "Choose a starter template", | ||
choices: [ | ||
{ title: "basic-template", value: CHOICES[0] }, | ||
{ title: "async-template", value: CHOICES[1] }, | ||
{ title: "promise-template", value: CHOICES[2] }, | ||
], | ||
}, | ||
], | ||
{ | ||
onCancel: () => { | ||
throw new Error(red("✖") + " Operation cancelled"); | ||
}, | ||
}, | ||
); | ||
} catch (cancelled) { | ||
if (cancelled instanceof Error) { | ||
console.log(cancelled.message); | ||
} | ||
process.exit(1); | ||
} | ||
|
||
const { packageName, shouldOverwrite, chooseProject } = result; | ||
|
||
const root = path.resolve(targetDir); | ||
|
||
if (shouldOverwrite) { | ||
fse.emptyDirSync(root); | ||
} else if (!fs.existsSync(root)) { | ||
fs.mkdirSync(root, { recursive: true }); | ||
} | ||
|
||
const pkg = { | ||
name: packageName ?? toValidPackageName(targetDir), | ||
version: "0.0.0", | ||
}; | ||
|
||
console.log("Setting up project..."); | ||
|
||
const templateRoot = path.join(__dirname, "../template"); | ||
const templateDir = path.resolve(templateRoot, chooseProject); | ||
|
||
// Read existing package.json from the root directory | ||
const packageJsonPath = path.join(root, "package.json"); | ||
|
||
// Read new package.json from the template directory | ||
const newPackageJsonPath = path.join(templateDir, "package.json"); | ||
const newPackageJson = JSON.parse( | ||
fs.readFileSync(newPackageJsonPath, "utf-8"), | ||
); | ||
|
||
fse.copySync(templateDir, root); | ||
|
||
fs.writeFileSync( | ||
packageJsonPath, | ||
JSON.stringify( | ||
{ | ||
...newPackageJson, | ||
...pkg, | ||
}, | ||
null, | ||
2, | ||
), | ||
); | ||
|
||
const manager = process.env.npm_config_user_agent ?? ""; | ||
const packageManager = /pnpm/.test(manager) | ||
? "pnpm" | ||
: /yarn/.test(manager) | ||
? "yarn" | ||
: "npm"; | ||
|
||
const commandsMap = { | ||
install: { | ||
pnpm: "pnpm install", | ||
yarn: "yarn", | ||
npm: "npm install", | ||
}, | ||
dev: { | ||
pnpm: "pnpm dev", | ||
yarn: "yarn dev", | ||
npm: "npm run dev", | ||
}, | ||
}; | ||
|
||
console.log(`\nDone. Now run:\n`); | ||
console.log(`${bold(green(`cd ${targetDir}`))}`); | ||
console.log(`${bold(green(commandsMap.install[packageManager]))}`); | ||
console.log(`${bold(green(commandsMap.dev[packageManager]))}`); | ||
console.log(); | ||
} | ||
|
||
init().catch((e) => { | ||
console.error(e); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Future TODO: make type=module and use swc instead of esbuild.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will take look into this.