Skip to content

Commit

Permalink
feat(cli): replace jest with vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
haoziqaq authored and BeADre committed Sep 18, 2022
1 parent ad01e43 commit 7ddbade
Show file tree
Hide file tree
Showing 83 changed files with 1,381 additions and 2,321 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: pnpm lint

- name: Run test cases
run: cd packages/varlet-ui && pnpm test
run: cd packages/varlet-ui && pnpm test -- -cov

- name: Report coverage to Codecov
uses: codecov/codecov-action@v1
Expand Down
66 changes: 24 additions & 42 deletions packages/varlet-cli/lib/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,25 @@
#!/usr/bin/env node
"use strict";
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var logger_1 = __importDefault(require("./shared/logger"));
var commander_1 = require("commander");
var dev_1 = require("./commands/dev");
var build_1 = require("./commands/build");
var useVite_1 = require("./commands/useVite");
var compile_1 = require("./commands/compile");
var create_1 = require("./commands/create");
var jest_1 = require("./commands/jest");
var lint_1 = require("./commands/lint");
var gen_1 = require("./commands/gen");
var preview_1 = require("./commands/preview");
var changelog_1 = require("./commands/changelog");
var release_1 = require("./commands/release");
var commitLint_1 = require("./commands/commitLint");
var program = new commander_1.Command();
program.version("varlet-cli ".concat(require('../package.json').version)).usage('<command> [options]');
const logger_1 = __importDefault(require("./shared/logger"));
const commander_1 = require("commander");
const dev_1 = require("./commands/dev");
const build_1 = require("./commands/build");
const useVite_1 = require("./commands/useVite");
const compile_1 = require("./commands/compile");
const create_1 = require("./commands/create");
const test_1 = require("./commands/test");
const lint_1 = require("./commands/lint");
const gen_1 = require("./commands/gen");
const preview_1 = require("./commands/preview");
const changelog_1 = require("./commands/changelog");
const release_1 = require("./commands/release");
const commitLint_1 = require("./commands/commitLint");
const program = new commander_1.Command();
program.version(`varlet-cli ${require('../package.json').version}`).usage('<command> [options]');
program
.command('dev')
.option('-f --force', 'Force dep pre-optimization regardless of whether deps have changed')
Expand All @@ -45,11 +29,11 @@ program.command('build').description('Build varlet site for production').action(
program
.command('build:vite')
.description('Use vite build app for production')
.action(function () { return (0, useVite_1.useVite)('build'); });
.action(() => (0, useVite_1.useVite)('build'));
program
.command('dev:vite')
.description('Use vite start server for development')
.action(function () { return (0, useVite_1.useVite)('dev'); });
.action(() => (0, useVite_1.useVite)('dev'));
program.command('preview').description('Preview varlet site for production').action(preview_1.preview);
program
.command('compile')
Expand All @@ -66,13 +50,12 @@ program
.option('-l, --locale', 'Generator internationalized files')
.action(create_1.create);
program
.command('jest')
.description('Run Jest in work directory')
.command('test')
.description('Run test in work directory')
.option('-w, --watch', 'Watch files for changes and rerun tests related to changed files')
.option('-wa, --watchAll', 'Watch files for changes and rerun all tests when something changes')
.option('-c, --component <componentName>', 'Test a specific component')
.option('-cc --clearCache', 'Clear test cache')
.action(jest_1.jest);
.option('-cov, --coverage', 'Generate the coverage')
.action(test_1.test);
program.command('gen <name>').description('Generate cli application').action(gen_1.gen);
program
.command('changelog')
Expand All @@ -86,10 +69,9 @@ program
.description('Release all packages and generate changelogs')
.action(release_1.release);
program.command('commit-lint <gitParams>').description('Lint commit message').action(commitLint_1.commitLint);
program.on('command:*', function (_a) {
var _b = __read(_a, 1), cmd = _b[0];
program.on('command:*', ([cmd]) => {
program.outputHelp();
logger_1.default.error("\nUnknown command ".concat(cmd, ".\n"));
logger_1.default.error(`\nUnknown command ${cmd}.\n`);
process.exitCode = 1;
});
program.parse();
12 changes: 6 additions & 6 deletions packages/varlet-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,27 @@
"@vue/babel-plugin-jsx": "1.1.1",
"@vue/compiler-sfc": "3.2.25",
"@vue/runtime-core": "3.2.25",
"babel-jest": "26.6.3",
"chalk": "^4.1.0",
"chokidar": "^3.5.2",
"commander": "^8.3.0",
"conventional-changelog": "^3.1.25",
"ejs": "^3.1.8",
"execa": "^5.0.0",
"vitest": "0.22.1",
"fs-extra": "^9.0.1",
"glob": "^7.2.0",
"hash-sum": "^2.0.0",
"inquirer": "^8.1.2",
"jest": "^26.6.3",
"less": "^3.12.2",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
"ora": "^5.4.0",
"semver": "^7.3.5",
"slash": "^3.0.0",
"ts-jest": "^26.5.1",
"typescript": "^4.4.4",
"vite": "3.0.4",
"vite-plugin-html": "^2.1.0",
"vue": "3.2.25",
"vue-jest": "^5.0.0-alpha.8"
"vue": "3.2.25"
},
"devDependencies": {
"@types/babel__core": "^7.1.12",
Expand All @@ -85,7 +82,10 @@
"peerDependencies": {
"@varlet/touch-emulator": "workspace:*",
"@vue/runtime-core": "3.2.16",
"@vue/test-utils": "^2.0.0-rc.6",
"@vue/test-utils": "^2.0.2",
"@vitest/coverage-c8": "0.22.1",
"jsdom": "^20.0.0",
"vitest": "0.22.1",
"clipboard": "^2.0.6",
"live-server": "^1.2.1",
"lodash-es": "^4.17.21",
Expand Down
8 changes: 6 additions & 2 deletions packages/varlet-cli/src/commands/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export async function runTask(taskName: string, task: () => any) {
}
}

export async function compile(cmd: { noUmd: boolean }) {
interface CompileCommandOptions {
noUmd?: boolean
}

export async function compile(options: CompileCommandOptions) {
process.env.NODE_ENV = 'compile'

await removeDir()
Expand All @@ -37,5 +41,5 @@ export async function compile(cmd: { noUmd: boolean }) {
await runTask('commonjs', () => compileModule('commonjs'))

process.env.TARGET_MODULE = 'umd'
!cmd.noUmd && (await runTask('umd', () => compileModule('umd')))
!options.noUmd && (await runTask('umd', () => compileModule('umd')))
}
16 changes: 8 additions & 8 deletions packages/varlet-cli/src/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { glob } from '../shared/fsUtils'
import { getVarletConfig } from '../config/varlet.config'
import { SRC_DIR } from '../shared/constant'

interface CmdTypes {
interface CreateCommandOptions {
name?: string
locale?: boolean
sfc?: boolean
Expand Down Expand Up @@ -57,11 +57,11 @@ const removeFiles = async (path: string) => {
;(await glob(`${path}/**/*.ejs`)).forEach((file) => removeSync(file))
}

export async function create(cmd: CmdTypes) {
export async function create(options: CreateCommandOptions) {
logger.title('\n📦📦 Create a Component ! \n')

const { name } = cmd.name
? cmd
const { name } = options.name
? options
: await prompt({
name: 'name',
message: 'Name of the component created: ',
Expand All @@ -77,8 +77,8 @@ export async function create(cmd: CmdTypes) {
}

// Determine whether the parameter carries internationalization.
const { locale } = cmd.locale
? cmd
const { locale } = options.locale
? options
: await prompt({
name: 'locale',
type: 'confirm',
Expand All @@ -89,8 +89,8 @@ export async function create(cmd: CmdTypes) {
createOptions.locale = locale

// Determine whether the parameter carries a component style
if (cmd.sfc || cmd.tsx) {
createOptions.style = cmd.sfc ? 'vue' : 'tsx'
if (options.sfc || options.tsx) {
createOptions.style = options.sfc ? 'vue' : 'tsx'
} else {
const { style } = await prompt({
name: 'style',
Expand Down
8 changes: 6 additions & 2 deletions packages/varlet-cli/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,14 @@ async function startServer(force: boolean | undefined) {
logger.success(`\n${isRestart ? 'Res' : 'S'}tart successfully!!!`)
}

export async function dev(cmd: { force?: boolean }) {
interface DevCommandOptions {
force?: boolean
}

export async function dev(options: DevCommandOptions) {
process.env.NODE_ENV = 'development'

ensureDirSync(SRC_DIR)

await startServer(cmd.force)
await startServer(options.force)
}
33 changes: 0 additions & 33 deletions packages/varlet-cli/src/commands/jest.ts

This file was deleted.

8 changes: 6 additions & 2 deletions packages/varlet-cli/src/commands/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ async function getReleaseType() {
return ret[name]
}

export async function release(cmd: { remote?: string }) {
interface ReleaseCommandOptions {
remote?: string
}

export async function release(options: ReleaseCommandOptions) {
try {
const currentVersion = require(resolve(CWD, 'package.json')).version

Expand Down Expand Up @@ -128,7 +132,7 @@ export async function release(cmd: { remote?: string }) {

if (!isPreRelease) {
await changelog()
await pushGit(expectVersion, cmd.remote)
await pushGit(expectVersion, options.remote)
}

logger.success(`Release version ${expectVersion} successfully!`)
Expand Down
33 changes: 33 additions & 0 deletions packages/varlet-cli/src/commands/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import logger from '../shared/logger.js'
import execa from 'execa'
import { VITEST_CONFIG } from '../shared/constant'

interface TestCommandOptions {
watch?: boolean
component?: string
coverage?: boolean
}

export async function test({ component, watch, coverage }: TestCommandOptions) {
process.env.NODE_ENV = 'test'

const args = ['--config', VITEST_CONFIG]

if (!watch) {
args.unshift('run')
}

if (coverage) {
args.push('--coverage')
}

if (component) {
args.push('--dir', `src/${component.trim()}`)
}

try {
await execa('vitest', args, { stdin: 'inherit', stdout: 'inherit', stderr: 'inherit' })
} catch (e: any) {
logger.error(e.toString())
}
}
42 changes: 0 additions & 42 deletions packages/varlet-cli/src/config/jest.config.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/varlet-cli/src/config/jest.media.mock.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/varlet-cli/src/config/jest.style.mock.ts

This file was deleted.

Loading

0 comments on commit 7ddbade

Please sign in to comment.