Skip to content

Commit

Permalink
fix(create-blocklet): validate project name compliance before creation(
Browse files Browse the repository at this point in the history
  • Loading branch information
Pengfei committed Dec 2, 2024
1 parent 8c3483a commit 0311323
Show file tree
Hide file tree
Showing 5 changed files with 1,283 additions and 23 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,18 @@ TBD
pnpm install

```

## Run the command

```bash
node packages/create-app/index.js test-demo
```

## Change Blocklet Server Command

Change the file `packages/create-app/lib/constant.js`

```
// your command alias
export const BLOCKLET_COMMAND = 'bn';
```
32 changes: 19 additions & 13 deletions packages/create-app/index.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
#!/usr/bin/env node

import ejs from 'ejs';
import { isValid, toTypeInfo, types } from '@arcblock/did';
import urlPathFriendly from '@blocklet/meta/lib/url-path-friendly.js';
import boxen from 'boxen';
import { fileURLToPath } from 'url';
import { execSync } from 'child_process';
import { cd, argv, fs, YAML, chalk, path } from 'zx';
import ejs from 'ejs';
import * as envfile from 'envfile';
import ora from 'ora';
import prompts from 'prompts';
import { isValid, toTypeInfo, types } from '@arcblock/did';
import * as envfile from 'envfile';
import { slugify } from 'transliteration';
import { fileURLToPath } from 'url';
import { argv, cd, chalk, fs, path, YAML } from 'zx';

import { echoBrand, echoDocument } from './lib/arcblock.js';
import { getUser } from './lib/index.js';
import { checkServerInstalled, checkServerRunning, checkSatisfiedVersion, getServerDirectory } from './lib/server.js';
import { getBlockletDidList } from './lib/did.js';
import { initGitRepo } from './lib/git.js';
import { getUser } from './lib/index.js';
import { checkSatisfiedVersion, checkServerInstalled, checkServerRunning, getServerDirectory } from './lib/server.js';
import {
checkLerna,
checkYarn,
copy,
emptyDir,
fuzzyQuery,
isEmpty,
isValidName,
isValidPackageName,
toValidPackageName,
fuzzyQuery,
checkLerna,
checkYarn,
} from './lib/utils.js';

const { yellow, red, green, cyan, blue, bold } = chalk;
Expand Down Expand Up @@ -247,18 +250,21 @@ async function init() {

let result = {};
const authorInfo = await getUser();
const transferName = urlPathFriendly.default(slugify(defaultProjectName)).toLowerCase();

try {
result = await prompts(
[
{
type: targetDir && !['.', './'].includes(targetDir) ? null : 'text',
type: isValidName(defaultProjectName) && targetDir && !['.', './'].includes(targetDir) ? null : 'text',
name: 'projectName',
message: 'Project name:',
initial: defaultProjectName,
initial: transferName,
onState: (state) => {
projectName = state.value.trim() || defaultProjectName;
projectName = state.value.trim() || transferName;
},
validate: (value) =>
isValidName(value) ? true : 'Please enter a valid project name, only a~z, A~Z, 0~9, - and _ are allowed.',
},
{
type: () => (!fs.existsSync(targetDir) || isEmpty(targetDir) ? null : 'confirm'),
Expand Down
4 changes: 4 additions & 0 deletions packages/create-app/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export function copy(src, dest) {
}
}

export function isValidName(name) {
return /^[a-zA-Z0-9][-a-zA-Z0-9_]{2,128}$/.test(name);
}

export function isValidPackageName(projectName) {
return /^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/.test(projectName);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/create-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"dependencies": {
"@arcblock/did": "^1.18.139",
"@blocklet/meta": "^1.16.33",
"@ocap/mcrypto": "^1.18.139",
"@ocap/util": "^1.18.139",
"boxen": "^7.1.1",
Expand All @@ -42,6 +43,7 @@
"prompts": "^2.4.2",
"semver": "^7.6.3",
"terminal-link": "^3.0.0",
"transliteration": "^2.3.5",
"zx": "^8.1.8"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 0311323

Please sign in to comment.