Skip to content

Commit

Permalink
fix: 修复脚手架
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyuang committed Jun 20, 2019
1 parent ffb2a00 commit b547961
Show file tree
Hide file tree
Showing 36 changed files with 155 additions and 157 deletions.
10 changes: 5 additions & 5 deletions docs/guide/hydrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter, StaticRouter, Route } from 'react-router-dom'
import defaultLayout from '@/layout'
import { GetinitialProps, getComponent } from 'ykfe-utils'
import { getWrappedComponent, getComponent } from 'ykfe-utils'
import { routes as Routes } from '../config/config.default'

const clientRender = async () => {
// 客户端渲染|水合
ReactDOM[window.__USESSR__ ? 'hydrate' : 'render'](
<BrowserRouter>
{
// 使用HOC使得csr首次进入页面以及csr/ssr切换路由时调用getInitialProps
{
// 使用高阶组件getWrappedComponent使得csr首次进入页面以及csr/ssr切换路由时调用getInitialProps
Routes.map(({ path, exact, Component }, key) => {
const ActiveComponent = Component()
const Layout = ActiveComponent.Layout || defaultLayout
return <Route exact={exact} key={key} path={path} render={() => {
const HOC = GetinitialProps(ActiveComponent)
return <Layout><HOC /></Layout>
const WrappedComponent = getWrappedComponent(ActiveComponent)
return <Layout><WrappedComponent /></Layout>
}} />
})
}
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const beginDocStream = stringToStream(beginDoc.replace('<!-- Start Injecting Sty

// 这里通过注入window.__USESSR__告诉客户端当前使用了服务端渲染,在HOC组件以及clientRender方法中用到了该属性
// 我们通过window.__INITIAL_DATA__将服务端获取的数据注入到页面中,作为客户端hydrate的初始数据
const initialData = !isCsr ? `<script>window.__USESSR__=true;window.__INITIAL_DATA__ =${serialize(ctx.serverData || {})};</script>` : ''
const initialData = !isCsr ? `<script>window.__USE_SSR__=true;window.__INITIAL_DATA__ =${serialize(ctx.serverData || {})};</script>` : ''

// 插入静态js文件资源
const injectSrcipt = config.injectSrcipt(chunkName).join('')
Expand Down
2 changes: 1 addition & 1 deletion example/ssr-with-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-ssr",
"version": "1.0.0",
"version": "1.1.0",
"dependencies": {
"egg": "^2.21.0",
"egg-bin": "^4.12.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/yk-cli/bin/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const fs_1 = __importDefault(require("fs"));
* @returns {Promise<void>}
*/
function init_app(option) {
console.log("当前应用名称: " + option.appname);
console.log('当前应用名称: ' + option.appname);
return new Promise((resolve, reject) => {
/** 如果文件重复则提示 是否删除 对应 create react app 的交互 */
if (fs_1.default.existsSync(`./${option.appname}`)) {
Expand Down
12 changes: 5 additions & 7 deletions packages/yk-cli/bin/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
const versionEffective_1 = require("./util/versionEffective");
const versionCompare_1 = require("./util/versionCompare");
const fileconfig_1 = require("./util/fileconfig");
const child_process_1 = require("child_process");
const path_1 = __importDefault(require("path"));
const download = require('download-git-repo');
Expand All @@ -21,10 +20,10 @@ function cacheMange(option) {
.then((value) => new Promise((resolve, reject) => {
/** 如果版本不一致 则直接更新缓存 */
if (!value) {
download(`github:zhusjfaker/egg-react-ssr#${fileconfig_1.config.branch}`, path_1.default.resolve(__dirname, "../cache"), (err) => {
download(`https://github.com/ykfe/egg-react-ssr.git#master`, path_1.default.resolve(__dirname, '../cache'), (err) => {
if (!err) {
const packagejson_path = path_1.default.resolve(__dirname, `../cache/example/ssr-with-${option.language === "javascript" ? "js" : "ts"}/package.json`);
const version = require(packagejson_path).version;
const packagejsonPath = path_1.default.resolve(__dirname, `../cache/example/ssr-with-${option.language === 'javascript' ? 'js' : 'ts'}/package.json`);
const version = require(packagejsonPath).version;
versionCompare_1.versionlog(option.language, version);
resolve();
}
Expand All @@ -35,20 +34,19 @@ function cacheMange(option) {
}
});
}
/** 版本一致跳过 */
else {
resolve();
}
})).then(() => new Promise((resolve, reject) => {
/** 将缓存中的对应的项目内容拷贝至 '项目名称' 文件夹 */
const task = child_process_1.spawn(`cp -rf ${__dirname}/../cache/example/ssr-with-${option.language === "javascript" ? "js" : "ts"} ./`, [], { cwd: `./`, shell: true });
const task = child_process_1.spawn(`cp -rf ${__dirname}/../cache/example/ssr-with-${option.language === 'javascript' ? 'js' : 'ts'} ./`, [], { cwd: `./`, shell: true });
task.on('close', (_code) => {
console.log('原始项目克隆成功....');
resolve();
});
})).then(() => new Promise((resolve, reject) => {
/** 当前执行目录下修改创建 '项目名称' 文件夹 */
const task = child_process_1.spawn(`mv ssr-with-${option.language === "javascript" ? "js" : "ts"} ${option.appname}`, [], { cwd: `./`, shell: true });
const task = child_process_1.spawn(`mv ssr-with-${option.language === 'javascript' ? 'js' : 'ts'} ${option.appname}`, [], { cwd: `./`, shell: true });
task.on('close', (code) => {
console.log('创建项目文件夹.....');
resolve();
Expand Down
14 changes: 7 additions & 7 deletions packages/yk-cli/bin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,20 @@ function appconfig(option) {
type: 'list',
message: '使用的开发语言',
name: 'language',
default: "javascript",
default: 'javascript',
choices: [
"javascript",
"typescript"
'javascript',
'typescript'
]
}, {
type: 'list',
message: '是否使用样式预处理器:',
name: 'style',
default: "less",
default: 'less',
choices: [
"less",
"sass",
"css"
'less',
'sass',
'css'
]
}]).then((answers) => {
option.appname = answers.appname;
Expand Down
2 changes: 1 addition & 1 deletion packages/yk-cli/bin/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fs_1 = __importDefault(require("fs"));
*/
function help() {
return new Promise((resolve, reject) => {
let content = fs_1.default.readFileSync(`${__dirname}/../help.txt`).toString("utf8");
let content = fs_1.default.readFileSync(`${__dirname}/../help.txt`).toString('utf8');
console.log(content);
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/yk-cli/bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const generator = (argv) => {
try {
switch (action) {
/** 构建项目 */
case "init":
case 'init':
/** 自检更新当前脚手架是否最新 */
await update_1.updatelocal(option);
/** 问询APP配置 */
Expand All @@ -35,13 +35,13 @@ const generator = (argv) => {
/** 处理 webpack */
await webpackconfig_1.webpack(option);
/** 处理 组件 */
if ((option.language === "javascript" && option.style !== "less") || (option.language === "typescript" && option.style !== "sass")) {
if ((option.language === 'javascript' && option.style !== 'less') || (option.language === 'typescript' && option.style !== 'sass')) {
await webcomponent_1.component(option);
}
console.log(`项目安装成功!`);
break;
/** 项目帮助 */
case "help":
case 'help':
await help_1.help();
break;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/yk-cli/bin/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const path_1 = __importDefault(require("path"));
*/
function packagejson(option) {
return new Promise((resolve, reject) => {
const tplpath = path_1.default.resolve(__dirname, "../tpl/package.json.nj");
const tplpath = path_1.default.resolve(__dirname, '../tpl/package.json.nj');
render_1.renderTemplate(tplpath, `./${option.appname}/package.json`, option);
console.log("原项目配置修改成功.....");
console.log('原项目配置修改成功.....');
resolve();
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/yk-cli/bin/util/fileconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
* 脚手架参数
*/
exports.config = {
ts_url: "https://raw.githubusercontent.com/zhusjfaker/egg-react-ssr/backup/example/ssr-with-ts/package.json",
js_url: "https://raw.githubusercontent.com/zhusjfaker/egg-react-ssr/backup/example/ssr-with-js/package.json",
branch: "backup"
ts_url: 'https://raw.githubusercontent.com/zhusjfaker/egg-react-ssr/backup/example/ssr-with-ts/package.json',
js_url: 'https://raw.githubusercontent.com/ykfe/egg-react-ssr/master/example/ssr-with-js/package.json',
branch: 'backup'
};
4 changes: 2 additions & 2 deletions packages/yk-cli/bin/util/readFileList.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const path_1 = __importDefault(require("path"));
function readFileList(dir, filesList = [], ext = null) {
const files = fs_1.default.readdirSync(dir);
files.forEach((item, index) => {
var fullPath = path_1.default.join(dir, item);
let fullPath = path_1.default.join(dir, item);
const stat = fs_1.default.statSync(fullPath);
if (stat.isDirectory()) {
readFileList(path_1.default.join(dir, item), filesList, ext); //递归读取文件
readFileList(path_1.default.join(dir, item), filesList, ext); // 递归读取文件
}
else {
if (ext && item.indexOf(ext) > -1) {
Expand Down
18 changes: 9 additions & 9 deletions packages/yk-cli/bin/util/versionCompare.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ const path_1 = __importDefault(require("path"));
* @returns {boolean} 是否一致
*/
function versionCompare(type, version) {
const version_file = path_1.default.resolve(__dirname, `../../${type}.version.json`);
const version_dir = path_1.default.resolve(__dirname, `../../cache/example/ssr-with-${type === "javascript" ? "js" : "ts"}/package.json`);
if (fs_1.default.existsSync(version_file) && fs_1.default.existsSync(version_dir)) {
const old_version = require(version_file).version;
return old_version === version;
const versionFile = path_1.default.resolve(__dirname, `../../${type}.version.json`);
const versionDir = path_1.default.resolve(__dirname, `../../cache/example/ssr-with-${type === 'javascript' ? 'js' : 'ts'}/package.json`);
if (fs_1.default.existsSync(versionFile) && fs_1.default.existsSync(versionDir)) {
const oldVersion = require(versionFile).version;
return oldVersion === version;
}
else {
return false;
Expand All @@ -34,8 +34,8 @@ exports.versionCompare = versionCompare;
* @param {string} version 版本号
*/
function versionlog(type, version) {
const version_file = path_1.default.resolve(__dirname, `../../${type}.version.json`);
fs_1.default.writeFileSync(version_file, JSON.stringify({ version: version }));
const versionFile = path_1.default.resolve(__dirname, `../../${type}.version.json`);
fs_1.default.writeFileSync(versionFile, JSON.stringify({ version: version }));
}
exports.versionlog = versionlog;
/**
Expand All @@ -46,9 +46,9 @@ exports.versionlog = versionlog;
* @returns {Promise<boolean>}
*/
async function deletecache(type) {
const version_file = `${type}.version.json`;
const versionFile = `${type}.version.json`;
return new Promise((resolve, reject) => {
const task = child_process_1.spawn(`rm -rf ./cache && rm -rf ./${version_file}`, [], { cwd: path_1.default.resolve(__dirname, "../.."), shell: true });
const task = child_process_1.spawn(`rm -rf ./cache && rm -rf ./${versionFile}`, [], { cwd: path_1.default.resolve(__dirname, '../..'), shell: true });
task.on('close', (code) => { resolve(true); });
});
}
Expand Down
6 changes: 3 additions & 3 deletions packages/yk-cli/bin/util/versionEffective.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const https_1 = __importDefault(require("https"));
*/
async function getVersionEffective(option) {
return new Promise((resolve, reject) => {
let url = option.language === "typescript" ? fileconfig_1.config.ts_url : fileconfig_1.config.js_url;
let content = "";
let url = option.language === 'typescript' ? fileconfig_1.config.ts_url : fileconfig_1.config.js_url;
let content = '';
let packagejson = null;
https_1.default.get(url, (res) => {
res.on('data', (buffer) => {
Expand All @@ -28,7 +28,7 @@ async function getVersionEffective(option) {
resolve(false);
});
res.on('end', async () => {
if (content !== "") {
if (content !== '') {
try {
packagejson = JSON.parse(content);
const version = packagejson.version;
Expand Down
34 changes: 15 additions & 19 deletions packages/yk-cli/bin/webcomponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const render_1 = require("./util/render");
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function convert(type) {
if (type === "sass") {
return "scss";
if (type === 'sass') {
return 'scss';
}
else {
return type;
Expand All @@ -24,44 +24,40 @@ function convert(type) {
*/
async function component(option) {
return new Promise((resolve, reject) => {
const webpath = path_1.default.resolve(__dirname, "../tpl/web");
const webpath = path_1.default.resolve(__dirname, '../tpl/web');
/** 递归获取web下所有的组件文件模板 */
const list = readFileList_1.readFileList(webpath, []);
list.forEach(p => {
/** 处理所有样式 css/less/scss 模板 */
if (p.indexOf('.css.nj') > -1) {
let filepath = null;
let filepath_perfix = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + "/", "").replace(".css.nj", "")}`);
let filepath_perfix = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + '/', '').replace('.css.nj', '')}`);
/** js 版本对应的是 *.less */
if (option.language === "javascript") {
filepath = filepath_perfix + ".less";
if (option.language === 'javascript') {
filepath = filepath_perfix + '.less';
}
/** ts 版本对应的是 *.module.scss */
else if (option.language === "typescript") {
filepath = filepath_perfix.indexOf('assets') > -1 ? filepath_perfix + ".scss" : filepath_perfix + ".module.scss";
else if (option.language === 'typescript') {
filepath = filepath_perfix.indexOf('assets') > -1 ? filepath_perfix + '.scss' : filepath_perfix + '.module.scss';
}
/** 把对应的 旧 less / module.scss 文件删除 写入新的 css / scss / less 文件 */
if (filepath && fs_1.default.existsSync(filepath)) {
fs_1.default.unlinkSync(filepath);
const newfilepath = `${filepath_perfix}${option.language === "typescript" && filepath.indexOf('assets') === -1 ? ".module" : ""}.${convert(option.style)}`;
const newfilepath = `${filepath_perfix}${option.language === 'typescript' && filepath.indexOf('assets') === -1 ? '.module' : ''}.${convert(option.style)}`;
render_1.renderTemplate(p, newfilepath, option);
}
}
/** 处理所有的react-javascript组件模板 */
else if (p.indexOf('.js.nj') > -1 && option.language === "javascript") {
let filepath = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + "/", "").replace(".js.nj", ".js")}`);
else if (p.indexOf('.js.nj') > -1 && option.language === 'javascript') {
let filepath = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + '/', '').replace('.js.nj', '.js')}`);
render_1.renderTemplate(p, filepath, option);
}
/** 处理所有的react-typescript组件模板 */
else if (p.indexOf('.tsx.nj') > -1 && option.language === "typescript") {
let filepath = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + "/", "").replace(".tsx.nj", ".tsx")}`);
else if (p.indexOf('.tsx.nj') > -1 && option.language === 'typescript') {
let filepath = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + '/', '').replace('.tsx.nj', '.tsx')}`);
render_1.renderTemplate(p, filepath, option);
}
/** 处理所有的模块类声明 */
else if (p.indexOf('.style.d.ts.nj') > -1) {
let filepath = null;
let filepath_perfix = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + "/", "").replace(".style.d.ts.nj", "")}`);
filepath = filepath_perfix + ".scss.d.ts";
let filepath_perfix = path_1.default.resolve(`./${option.appname}/web/${p.replace(webpath + '/', '').replace('.style.d.ts.nj', '')}`);
filepath = filepath_perfix + '.scss.d.ts';
/** 替换less.d.ts || css.d.ts */
if (filepath && fs_1.default.existsSync(filepath)) {
fs_1.default.unlinkSync(filepath);
Expand Down
4 changes: 2 additions & 2 deletions packages/yk-cli/bin/webpackconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ async function webpack(option) {
];
filelist.forEach(p => {
const tplpath = path_1.default.resolve(__dirname, '..', p);
const filepath = `./${option.appname}/${p.replace("tpl/", "").replace(".nj", "")}`;
const filepath = `./${option.appname}/${p.replace('tpl/', '').replace('.nj', '')}`;
render_1.renderTemplate(tplpath, filepath, option);
});
console.log("webpack设置成功.....");
console.log('webpack设置成功.....');
resolve();
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/yk-cli/src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export function cacheMange (option: Optional): Promise<void> {

return getVersionEffective(option)
.then((value) => new Promise<void>((resolve, reject) => {
console.log('xxx',value)
/** 如果版本不一致 则直接更新缓存 */
if (!value) {
download(`github:zhusjfaker/egg-react-ssr#${config.branch}`, path.resolve(__dirname, '../cache'), (err: any) => {
download(`https://github.com/ykfe/egg-react-ssr.git`, path.resolve(__dirname, '../cache'), (err: any) => {
if (!err) {
const packagejsonPath = path.resolve(__dirname, `../cache/example/ssr-with-${option.language === 'javascript' ? 'js' : 'ts'}/package.json`)
const version = require(packagejsonPath).version
Expand Down
2 changes: 1 addition & 1 deletion packages/yk-cli/src/util/fileconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
*/
export const config = {
ts_url: 'https://raw.githubusercontent.com/zhusjfaker/egg-react-ssr/backup/example/ssr-with-ts/package.json',
js_url: 'https://raw.githubusercontent.com/zhusjfaker/egg-react-ssr/backup/example/ssr-with-js/package.json',
js_url: 'https://raw.githubusercontent.com/ykfe/egg-react-ssr/master/example/ssr-with-js/package.json',
branch: 'backup'
}
2 changes: 1 addition & 1 deletion packages/yk-cli/types/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Optional } from "./interface/option";
import { Optional } from './interface/option';
/**
* 应用初始化函数
*
Expand Down
2 changes: 1 addition & 1 deletion packages/yk-cli/types/cache.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Optional } from "./interface/option";
import { Optional } from './interface/option';
/**
* 缓存管理
*
Expand Down
2 changes: 1 addition & 1 deletion packages/yk-cli/types/config.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Optional } from "./interface/option";
import { Optional } from './interface/option';
/**
* 应用配置环节
* 获取全局配置 option
Expand Down
1 change: 1 addition & 0 deletions packages/yk-cli/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#!/usr/bin/env node
export {};
Loading

0 comments on commit b547961

Please sign in to comment.