feat(cli): find root dir

This commit is contained in:
陈嘉涵 2019-12-12 17:19:31 +08:00
parent 71415e467d
commit c86d3d166f
5 changed files with 35 additions and 21 deletions

View File

@ -1,12 +1,12 @@
import { join } from 'path';
import { exec } from 'shelljs';
import { ROOT } from '../common/constant';
export function changelog(dist: string, cmd: { tag?: string }) {
const basepath = process.cwd();
const tag = cmd.tag || 'v1.0.0';
exec(`
basepath=${basepath}
basepath=${ROOT}
github_changelog_generator \
--header-label "# 更新日志" \
@ -18,6 +18,6 @@ export function changelog(dist: string, cmd: { tag?: string }) {
--no-author \
--no-unreleased \
--since-tag ${tag} \
-o ${join(basepath, dist)}
-o ${join(ROOT, dist)}
`);
}

View File

@ -1,7 +1,7 @@
import { runCLI } from 'jest';
import { setNodeEnv } from '../common';
import { genPackageEntry } from '../compiler/gen-package-entry';
import { CWD, JEST_CONFIG_FILE, PACKAGE_ENTRY_FILE } from '../common/constant';
import { ROOT, JEST_CONFIG_FILE, PACKAGE_ENTRY_FILE } from '../common/constant';
export function test(command: any) {
setNodeEnv('test');
@ -11,10 +11,10 @@ export function test(command: any) {
});
const config = {
rootDir: CWD,
rootDir: ROOT,
watch: command.watch,
config: JEST_CONFIG_FILE
} as any;
runCLI(config, [CWD]);
runCLI(config, [ROOT]);
}

View File

@ -1,14 +1,28 @@
import { get } from 'lodash';
import { join, isAbsolute } from 'path';
import { existsSync } from 'fs-extra';
import { join, dirname, isAbsolute } from 'path';
function findRootDir(dir: string): string {
if (dir === '/') {
return '/';
}
if (existsSync(join(dir, 'vant.config.js'))) {
return dir;
}
return findRootDir(dirname(dir));
}
export const CWD = process.cwd();
export const ES_DIR = join(CWD, 'es');
export const LIB_DIR = join(CWD, 'lib');
export const DOCS_DIR = join(CWD, 'docs');
export const SITE_DIST_DIR = join(CWD, 'site');
export const VANT_CONFIG_FILE = join(CWD, 'vant.config.js');
export const PACKAGE_JSON_FILE = join(CWD, 'package.json');
export const WEBPACK_CONFIG_FILE = join(CWD, 'webpack.config.js');
export const ROOT = findRootDir(CWD);
export const ES_DIR = join(ROOT, 'es');
export const LIB_DIR = join(ROOT, 'lib');
export const DOCS_DIR = join(ROOT, 'docs');
export const SITE_DIST_DIR = join(ROOT, 'site');
export const VANT_CONFIG_FILE = join(ROOT, 'vant.config.js');
export const PACKAGE_JSON_FILE = join(ROOT, 'package.json');
export const WEBPACK_CONFIG_FILE = join(ROOT, 'webpack.config.js');
export const DIST_DIR = join(__dirname, '../../dist');
export const CONFIG_DIR = join(__dirname, '../config');
@ -19,10 +33,10 @@ export const SITE_MODILE_SHARED_FILE = join(DIST_DIR, 'site-mobile-shared.js');
export const SITE_DESKTOP_SHARED_FILE = join(DIST_DIR, 'site-desktop-shared.js');
export const STYPE_DEPS_JSON_FILE = join(DIST_DIR, 'style-deps.json');
export const JEST_CONFIG_FILE = join(CONFIG_DIR, 'jest.config.js');
export const BABEL_CONFIG_FILE = join(CONFIG_DIR, 'babel.config.js');
export const POSTCSS_CONFIG_FILE = join(CONFIG_DIR, 'postcss.config.js');
export const JEST_INIT_FILE = join(CONFIG_DIR, 'jest.init.js');
export const JEST_CONFIG_FILE = join(CONFIG_DIR, 'jest.config.js');
export const JEST_TRANSFORM_FILE = join(CONFIG_DIR, 'jest.transform.js');
export const JEST_FILE_MOCK_FILE = join(CONFIG_DIR, 'jest.file-mock.js');
export const JEST_STYLE_MOCK_FILE = join(CONFIG_DIR, 'jest.style-mock.js');
@ -47,10 +61,10 @@ function getSrcDir() {
return srcDir;
}
return join(CWD, srcDir);
return join(ROOT, srcDir);
}
return join(CWD, 'src');
return join(ROOT, 'src');
}
export const SRC_DIR = getSrcDir();

View File

@ -4,7 +4,7 @@ import { join } from 'path';
import { transformFileAsync } from '@babel/core';
import { removeSync, outputFileSync, existsSync } from 'fs-extra';
import { replaceExt } from '../common';
import { CWD, DIST_DIR } from '../common/constant';
import { ROOT, DIST_DIR } from '../common/constant';
type Options = {
// whether to fouce reload babel config
@ -14,7 +14,7 @@ type Options = {
const TEMP_BABEL_CONFIG = join(DIST_DIR, 'babel.config.js');
function genTempBabelConfig() {
const { config } = findBabelConfig.sync(CWD);
const { config } = findBabelConfig.sync(ROOT);
const content = `module.exports = function (api) {
api.cache.never();

View File

@ -4,7 +4,7 @@ import sass from 'sass';
import { resolve } from 'path';
import { VueLoaderPlugin } from 'vue-loader';
import {
CWD,
ROOT,
STYLE_EXTS,
SCRIPT_EXTS,
POSTCSS_CONFIG_FILE
@ -63,7 +63,7 @@ export const baseConfig = {
{
loader: 'less-loader',
options: {
paths: [resolve(CWD, 'node_modules')]
paths: [resolve(ROOT, 'node_modules')]
}
}
]