Skip to content

fix: 修复 devui-cli #206

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

Merged
merged 1 commit into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"eslint-plugin-vue": "^7.11.1",
"husky": "^7.0.4",
"lint-staged": "^11.0.0",
"npm-run-all": "^4.1.5",
"stylelint": "^13.13.1",
"stylelint-config-recommended-scss": "^4.3.0",
"stylelint-config-standard": "^22.0.0",
Expand Down
5 changes: 2 additions & 3 deletions packages/devui-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"homepage": "https://github.com/DevCloudFE/vue-devui",
"license": "MIT",
"main": "lib/bin.js",
"types": "types/bin.d.ts",
"types": "types/config.d.ts",
"bin": {
"devui": "lib/bin.js"
},
Expand All @@ -25,9 +25,8 @@
},
"scripts": {
"dev": "esbuild --bundle ./src/bin.ts --format=cjs --platform=node --outfile=./lib/bin.js --external:esbuild --minify-whitespace --watch",
"build": "npm run build:lib & npm run build:dts",
"build": "run-p build:lib",
"build:lib": "rimraf ./lib && esbuild --bundle ./src/bin.ts --format=cjs --platform=node --outfile=./lib/bin.js --external:esbuild --minify-whitespace",
"build:dts": "rimraf ./types && tsc -p ./tsconfig.json",
"cli": "node ./lib/bin.js"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/devui-cli/src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env node
import { Command } from 'commander'
import type { CliConfig } from '../types/config'
import baseAction from './commands/base'
import createAction, { validateCreateType } from './commands/create'
import { CliConfig, detectCliConfig } from './shared/config'
import { detectCliConfig } from './shared/config'
import { VERSION } from './shared/constant'
import {
DEFAULT_CLI_CONFIG_FILE_NAME
Expand Down
68 changes: 34 additions & 34 deletions packages/devui-cli/src/commands/base.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,68 @@
import { existsSync, statSync } from 'fs-extra'
import { dirname, extname, isAbsolute, resolve } from 'path'
import prompts from 'prompts'
import { mergeCliConfig } from '../shared/config'
import { CWD } from '../shared/constant'
import generateConfig from '../shared/generate-config'
import logger from '../shared/logger'
import { dynamicImport, onPromptsCancel } from '../shared/utils'
import buildAction from './build'
import createAction from './create'
import { existsSync, statSync } from 'fs-extra';
import { dirname, extname, isAbsolute, resolve } from 'path';
import prompts from 'prompts';
import { mergeCliConfig } from '../shared/config';
import { CWD } from '../shared/constant';
import generateConfig from '../shared/generate-config';
import logger from '../shared/logger';
import { dynamicImport, onPromptsCancel } from '../shared/utils';
import buildAction from './build';
import createAction from './create';

function getActions() {
const actionMap = new Map<string, prompts.Choice & { action: Function }>()
const actionMap = new Map<string, prompts.Choice & { action: Function; }>();
actionMap.set('create', {
title: 'create',
value: 'create',
selected: true,
action: createAction
})
actionMap.set('build', { title: 'build', value: 'build', action: buildAction })
});
actionMap.set('build', { title: 'build', value: 'build', action: buildAction });

return actionMap
return actionMap;
}

export type BaseCmd = {
init?: boolean
config?: string
}
};

export default async function baseAction(cmd: BaseCmd) {
if (cmd.init) {
return generateConfig()
return generateConfig();
}

loadCliConfig(cmd)
loadCliConfig(cmd);

selectCommand()
selectCommand();
}

export function loadCliConfig(cmd: Pick<BaseCmd, 'config'>) {
if (!cmd.config) return
if (!cmd.config) return;

let configPath = resolve(CWD, cmd.config)
const configPath = resolve(CWD, cmd.config);

if (!existsSync(configPath)) {
logger.error(`The path "${configPath}" not exist.`)
process.exit(1)
logger.error(`The path "${configPath}" not exist.`);
process.exit(1);
}

if (statSync(configPath).isDirectory() || !['.js', '.ts'].includes(extname(configPath))) {
logger.error(`The path "${configPath}" is not a ".js" or ".ts" file.`)
process.exit(1)
logger.error(`The path "${configPath}" is not a ".js" or ".ts" file.`);
process.exit(1);
}

const config = dynamicImport(configPath)
if (!isAbsolute(config.cwd)) {
config.cwd = resolve(dirname(configPath), config.cwd)
const config = dynamicImport(configPath);
if (config.cwd && !isAbsolute(config.cwd)) {
config.cwd = resolve(dirname(configPath), config.cwd);
}

mergeCliConfig(config)
mergeCliConfig(config);
}

async function selectCommand() {
const actions = getActions()
let result: any = {}
const actions = getActions();
let result: any = {};

try {
result = await prompts(
Expand All @@ -77,11 +77,11 @@ async function selectCommand() {
{
onCancel: onPromptsCancel
}
)
);
} catch (e: any) {
logger.error(e.message)
process.exit(1)
logger.error(e.message);
process.exit(1);
}

actions.get(result.command)!.action()
actions.get(result.command)!.action();
}
62 changes: 1 addition & 61 deletions packages/devui-cli/src/shared/config.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,11 @@
import { readdirSync, statSync } from 'fs-extra'
import { merge } from 'lodash-es'
import { resolve } from 'path'
import type { CliConfig } from '../../types/config'
import { loadCliConfig } from '../commands/base'
import { CWD } from './constant'
import { DEFAULT_CLI_CONFIG_NAME } from './generate-config'

export type CliConfig = {
/**
* current workspace directory
*
* ***Should be the root directory of the component library.***
*
* @default process.cwd()
*/
cwd: string
/**
* Generate the root directory of component.
*
* ***Note that the path should be based on the `cwd` of configuration item.***
*
* @default .
*/
componentRootDir: string
/**
* category of component
*
* @default ['通用', '导航', '反馈', '数据录入', '数据展示', '布局']
*/
componentCategories: string[]
/**
* prefix of the component library
*
* @default ''
*/
libPrefix: string
/**
* component style file suffix of the component library
*
* @default .css
*/
libStyleFileSuffix: string
/**
* component class prefix of the component library
*/
libClassPrefix: string
/**
* component library entry file name
*
* @default index
*/
libEntryFileName: string
/**
* Generate the root directory of the lib entry file.
*
* ***Note that the path should be based on the `cwd` of configuration item.***
*
* @default .
*/
libEntryRootDir: string
/**
* version of component library
*
* @default 0.0.0
*/
version: string
}

export const cliConfig: CliConfig = {
cwd: CWD,
componentRootDir: '.',
Expand Down
3 changes: 2 additions & 1 deletion packages/devui-cli/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { camelCase, upperFirst } from 'lodash-es'
import { extname, relative, resolve } from 'path'
import { coreFileName } from '../templates/component/utils'
import { cliConfig } from './config'
import { PKG_NAME } from './constant'

export function bigCamelCase(str: string) {
return upperFirst(camelCase(str))
Expand Down Expand Up @@ -35,7 +36,7 @@ export function dynamicImport(path: string) {
outfile: tempPath,
platform: 'node',
format: 'cjs',
external: ['esbuild', 'dev-cli']
external: ['esbuild', PKG_NAME]
})

const config = require(relativePath).default ?? {}
Expand Down
4 changes: 2 additions & 2 deletions packages/devui-cli/src/templates/base/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CliConfig } from "../../shared/config";
import { PKG_NAME } from "../../shared/constant";
import type { CliConfig } from '../../../types/config';
import { PKG_NAME } from '../../shared/constant';

export default function genConfigTemplate(config: Partial<CliConfig> = {}) {
return `\
Expand Down
5 changes: 2 additions & 3 deletions packages/devui-cli/src/templates/component/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ export const directiveName = (name: string) => bigCamelCase(name + 'Directive')

export async function getComponentMetaFiles() {
return glob('./**/meta.json', {
cwd: cliConfig.cwd,
absolute: true,
deep: 2
cwd: cliConfig.componentRootDir,
absolute: true
})
}

Expand Down
2 changes: 1 addition & 1 deletion packages/devui-cli/src/templates/lib-entry/lib-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default function genLibEntryTemplate(componentsMeta: ComponentMeta[]) {
return `\
import type { App } from 'vue'

${imports.join('\n')}
${imports.join('\n') || '// Not find components.'}

const installs = [
\t${installs.join(',\n\t')}
Expand Down
7 changes: 3 additions & 4 deletions packages/devui-cli/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"declaration": true,
"emitDeclarationOnly": true,
"downlevelIteration": true,
"declarationDir": "./types",
"typeRoots": ["./node_modules/@types", "./types"],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
Expand All @@ -20,6 +18,7 @@
},
"include": [
"./src/**/*.ts",
"./src/**/*.d.ts"
"./src/**/*.d.ts",
"./types/**/*.d.ts"
]
}
10 changes: 10 additions & 0 deletions packages/devui-vue/dc.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineCliConfig } from 'devui-cli';

export default defineCliConfig({
componentRootDir: './devui',
libClassPrefix: 'd',
libEntryFileName: 'vue-devui',
libEntryRootDir: './devui',
libPrefix: 'D',
libStyleFileSuffix: '.scss'
})
1 change: 0 additions & 1 deletion packages/devui-vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"generate:theme": "node ./devui-cli/index.js generate:theme",
"generate:dts": "node ./devui-cli/index.js generate:dts",
"copy": "cp package.json build && cp ../../README.md build && cp devui/theme/theme.scss build/theme",
"clean:cli": "npm uninstall -g devui-cli & npm uninstall -g vue-devui",
"cli:create": "node ./devui-cli/index.js create -t component",
"predev": "node ./devui-cli/index.js create -t vue-devui --ignore-parse-error",
"prebuild": "node ./devui-cli/index.js create -t vue-devui --ignore-parse-error"
Expand Down
Loading