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

feat: migrate create command to standalone package #27

Merged
merged 14 commits into from
Nov 13, 2024
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@
/examples/*/src/.tailwindcss
/examples/*/dist
/examples/*/node_modules
/create-tnf/node_modules
/create-tnf/dist
8 changes: 8 additions & 0 deletions create-tnf/.fatherrc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from 'father';

export default defineConfig({
cjs: {
input: 'src',
output: 'dist',
},
});
20 changes: 20 additions & 0 deletions create-tnf/bin/create-tnf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env node

// patch console for debug
// ref: https://remysharp.com/2014/05/23/where-is-that-console-log
if (process.env.DEBUG_CONSOLE) {
['log', 'warn', 'error'].forEach((method) => {
const old = console[method];
console[method] = function () {
let stack = new Error().stack.split(/\n/);
// Chrome includes a single "Error" line, FF doesn't.
if (stack[0].indexOf('Error') === 0) {
stack = stack.slice(1);
}
const args = [].slice.apply(arguments).concat([stack[1].trim()]);
return old.apply(console, args);
};
});
}

require('../dist');
29 changes: 29 additions & 0 deletions create-tnf/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@umijs/create-tnf",
sorrycc marked this conversation as resolved.
Show resolved Hide resolved
"version": "0.1.2",
"description": "A standalone create command for TNF.",
"bin": {
"create-tnf": "bin/create-tnf.js"
},
"scripts": {
"build": "father build"
},
"files": [
"bin",
"dist",
"templates"
],
"dependencies": {
"@clack/prompts": "^0.7.0",
"pathe": "^1.1.2"
},
"devDependencies": {
"father": "^4.5.1",
"typescript": "^5.6.3"
},
"keywords": [
"tnf",
"create",
"project"
]
}
2 changes: 2 additions & 0 deletions create-tnf/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const MIN_NODE_VERSION = 18;
export const FRAMEWORK_NAME = 'tnf';
1 change: 1 addition & 0 deletions src/create.ts → create-tnf/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export async function create({
template?: string;
}) {
intro('Creating a new project...');

const templatesPath = path.join(__dirname, '../templates');
const templateList = fs
.readdirSync(templatesPath)
Expand Down
30 changes: 30 additions & 0 deletions create-tnf/src/fishkit/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export function setNoDeprecation() {
// Use magic to suppress node deprecation warnings
// See: https://github.com/nodejs/node/blob/6311de332223e855e7f1ce03b7c920f51f308e95/lib/internal/process/warning.js#L95
// @ts-ignore
process.noDeprecation = '1';
}

export function checkVersion(minVersion: number, message?: string) {
const ver = parseInt(process.version.slice(1));
const isOdd = ver % 2 === 1;
if (isOdd) {
console.error(
`Odd node version is not supported, your node version is ${ver}.`,
);
process.exit(1);
}
if (ver < minVersion) {
console.error(
message ||
`Your node version ${ver} is not supported, please upgrade to ${minVersion} or above.`,
);
process.exit(1);
}
}

export function setNodeTitle(name: string) {
if (process.title === 'node') {
process.title = name;
}
}
30 changes: 30 additions & 0 deletions create-tnf/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from 'assert';
import yargsParser from 'yargs-parser';
import { FRAMEWORK_NAME, MIN_NODE_VERSION } from './constants.js';
import { create } from './create.js';
import {
checkVersion,
setNoDeprecation,
setNodeTitle,
} from './fishkit/node.js';

async function run(cwd: string) {
const argv = yargsParser(process.argv.slice(1));
const cmd = argv._[0];
assert(cmd, 'Command is required');

return create({
cwd: cwd,
name: argv._[1] as string | undefined,
template: argv.template,
});
}

setNoDeprecation();
sorrycc marked this conversation as resolved.
Show resolved Hide resolved
checkVersion(MIN_NODE_VERSION);
setNodeTitle(FRAMEWORK_NAME);

run(process.cwd()).catch((err) => {
console.error(err.message);
process.exit(1);
});
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions create-tnf/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "@total-typescript/tsconfig/bundler/no-dom",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["src"]
}
16 changes: 16 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
packages:
- "."
- "create-tnf"
- "examples/*"
2 changes: 1 addition & 1 deletion scripts/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import 'zx/globals';
);

console.log('update templates');
const templateDir = path.join(__dirname, '../templates');
const templateDir = path.join(__dirname, '../create-tnf/templates');
const templateDirs = fs
.readdirSync(templateDir)
.filter((dir) => fs.statSync(path.join(templateDir, dir)).isDirectory());
Expand Down
7 changes: 0 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ async function run(cwd: string) {
const cmd = argv._[0];
assert(cmd, 'Command is required');
switch (cmd) {
case 'create':
const { create } = await import('./create.js');
return create({
cwd: cwd,
name: argv._[1] as string | undefined,
template: argv.template,
});
case 'build':
const { build } = await import('./build.js');
return build({
Expand Down
Loading