Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from ice-lab/release/2.5.0
Browse files Browse the repository at this point in the history
release/2.5.0
  • Loading branch information
imsobear committed Aug 13, 2019
2 parents fd18a80 + fff7b25 commit 59d1fc2
Show file tree
Hide file tree
Showing 64 changed files with 1,120 additions and 75 deletions.
12 changes: 12 additions & 0 deletions packages/ice-devtools/CHANGELOG.md → CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
# Changelog

## 2.5.0

- [feat] 支持 react + ts 模板,移除 angular 模板
- [feat] 支持 rax 模板,模板渲染支持 index.js.ejs
- [feat] 支持通过环境变量自定义并发个数
- [fix] 修复包未发布的判断逻辑
- [fix] 生成物料数据时 source.registry 兼容 env
- [fix] 优化 npm 未发布时的错误信息 alibaba/ice#2631

## 2.4.2

- [fix] 修复 idev 初始化出来的组件模板 package.json 中 componentConfig 中的 name 和 adaptor 中的 name 不一致的 bug

## 2.4.1

- [fix] 修复 screenshot url 取值的优先级 bug

## 2.4.0

- [feat] 移除 .template 的设计
- [refactor] screentshot/homepage 两个字段的生成逻辑优化,截图功能提供单独的 CLI 工具 @ice/screenshot
- [refactor] utils 相关代码优化
Expand Down
19 changes: 11 additions & 8 deletions packages/ice-devtools/lib/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path');
const glob = require('glob');
const mkdirp = require('mkdirp');
const BluebirdPromise = require('bluebird');
const getUnpkgHost = require('ice-npm-utils').getUnpkgHost;
const { getUnpkgHost, getNpmRegistry } = require('ice-npm-utils');

const generateI18nData = require('../utils/i18n');
const validate = require('../utils/validate');
Expand Down Expand Up @@ -135,16 +135,19 @@ function generateMaterialsData(files, targetDir, type) {
const pkg = JSON.parse(fs.readFileSync(path.join(targetDir, pkgPath)));

const materialConfig = pkg[`${type}Config`] || {};
const unpkgHost = getUnpkgHost(pkg.name);
const npmName = pkg.name;
const unpkgHost = getUnpkgHost(npmName);


// 兼容 snapshot 字段
const screenshot = materialConfig.screenshot
|| materialConfig.snapshot
|| (hasScreenshot(path.dirname(pkgPath)) ? `${unpkgHost}/${pkg.name}@${pkg.version}/screenshot.png` : '');
|| (hasScreenshot(path.dirname(pkgPath)) ? `${unpkgHost}/${npmName}@${pkg.version}/screenshot.png` : '');

const registry =
(pkg.publishConfig && pkg.publishConfig.registry) ||
DEFAULT_REGISTRY;
(pkg.publishConfig && pkg.publishConfig.registry)
|| getNpmRegistry(npmName)
|| DEFAULT_REGISTRY;

// generate i18n data
const i18nData = generateI18nData({ title: materialConfig.title, description: pkg.description });
Expand All @@ -154,14 +157,14 @@ function generateMaterialsData(files, targetDir, type) {
name: materialConfig.name,
title: i18nData['zh-CN'].title || i18nData['en-US'].title,
description: i18nData['zh-CN'].description || i18nData['en-US'].description,
homepage: pkg.homepage || `${unpkgHost}/${pkg.name}@${pkg.version}/build/index.html`,
homepage: pkg.homepage || `${unpkgHost}/${npmName}@${pkg.version}/build/index.html`,
// TODO: 老物料展示依赖 categories,下个版本删除
categories: materialConfig.categories || [],
category: materialConfig.category,
repository: (pkg.repository && pkg.repository.url) || pkg.repository,
source: {
type: 'npm',
npm: pkg.name,
npm: npmName,
version: pkg.version,
registry,
author: pkg.author,
Expand All @@ -184,7 +187,7 @@ function generateMaterialsData(files, targetDir, type) {

// 并行从 npm 查询包信息并补全数据
// 实际并行数是 concurrency * 3(block+component+scaffold)
const concurrency = 20;
const concurrency = Number(process.env.CONCURRENCY) || 20;
logger.info(`通过 npm 查询 ${type} 信息开始,个数:${result.length},并行个数:${concurrency}`);

// 根据 npm 信息补全物料数据:publishTime, updateTime
Expand Down
8 changes: 4 additions & 4 deletions packages/ice-devtools/lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ async function initAsk(options = {}) {
name: '@icedesign/ice-vue-material-template (Vue 标准模板)',
value: '@icedesign/ice-vue-material-template',
},
{
name: '@icedesign/ice-angular-material-template (Angular 标准模板)',
value: '@icedesign/ice-angular-material-template',
},
// {
// name: '@icedesign/ice-angular-material-template (Angular 标准模板)',
// value: '@icedesign/ice-angular-material-template',
// },
],
},
]));
Expand Down
3 changes: 2 additions & 1 deletion packages/ice-devtools/lib/material/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = async function addMaterial(cwd, opts = {}) {
templatePath: src,
standalone,
materialConfig,
// init single material
initMaterial,
} = opts;

Expand All @@ -41,7 +42,7 @@ module.exports = async function addMaterial(cwd, opts = {}) {
dest,
npmName,
materialConfig,
transformRegexp: initMaterial ? /.+/ : /_package.json/,
transformRegexp: initMaterial ? /.+/ : /_package.json/, // only transfrom _package.json
skipGitIgnore: !standalone, // 物料仓库中,不处理 _gitignore 文件
});

Expand Down
12 changes: 10 additions & 2 deletions packages/ice-devtools/lib/material/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,15 @@ async function generateExample(cwd, templatePath, materialConfig) {
const pkg = pkgJSON.getPkgJSON(cwd);

// [block, component, scaffold]
const types = MATERIAL_TYPES.filter((type) => fs.statSync(path.join(templatePath, type)).isDirectory());
const types = MATERIAL_TYPES.filter((type) => {
try {
return fs.statSync(path.join(templatePath, type)).isDirectory();
} catch (error) {
// block maybe not exist in template
logger.warn(error.message);
return false;
}
});

for (let i = 0; i < types.length; i++) {
const type = types[i];
Expand All @@ -113,7 +121,7 @@ async function generateExample(cwd, templatePath, materialConfig) {
description: '示例',
skipGitIgnore: true,
materialConfig,
transformRegexp: /_package.json/, // only transfrom _package.json
transformRegexp: /_package.json/,
};

logger.verbose(`generateExample -> ${type}`, options);
Expand Down
2 changes: 1 addition & 1 deletion packages/ice-devtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ice-devtools",
"version": "2.4.2",
"version": "2.5.0",
"description": "ice 物料开发者工具",
"main": "bin/ice-devtools.js",
"bin": {
Expand Down
54 changes: 17 additions & 37 deletions packages/ice-devtools/utils/npm.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const kebabCase = require('kebab-case');
const chalk = require('chalk');
const spawn = require('cross-spawn');
const { getNpmInfo, isAliNpm } = require('ice-npm-utils');
const { isAliNpm } = require('ice-npm-utils');
const request = require('request-promise-native');

/**
* 获取 npm 包名
Expand All @@ -21,8 +21,20 @@ function generateNpmNameByPrefix(name, npmPrefix) {
* [code, resute]
*/
function getNpmTime(npm, version = 'latest') {
return getNpmInfo(npm)
.then((data) => {
// 这里不使用 ice-npm-utils 里的 getNpmRegistry 原因:
// 发布到私有 npm,走 env 自定义逻辑;发布到官方 npm,走官方源的逻辑
// getNpmRegistry 默认是淘宝源,同步官方源有延迟,因此不能用
let registry = 'https://registry.npmjs.org';
if (process.env.REGISTRY) {
registry = process.env.REGISTRY;
} else if (isAliNpm(npm)) {
registry = 'https://registry.npm.alibaba-inc.com';
}
const url = `${registry}/${npm}`;

return request.get(url)
.then((response) => {
const data = JSON.parse(response);
if (!data.time) {
console.error(chalk.red('time 字段不存在'));
return Promise.reject(new Error(`${npm}@${version} time 字段不存在`));
Expand All @@ -40,6 +52,7 @@ function getNpmTime(npm, version = 'latest') {
if (
(err.response && err.response.status === 404)
|| err.message === 'Not found' // tnpm
|| /not found/i.test(err.message) // tnpm
|| err.message === 'not_found' // npm
) {
// 这种情况是该 npm 包名一次都没有发布过
Expand All @@ -50,40 +63,7 @@ function getNpmTime(npm, version = 'latest') {
});
}

/**
* get NPM registry
*
* @returns {string} npm registry url
*/
function getNpmRegistry(npmName) {
// return REGISTRY env variable
if (process.env.REGISTRY) return process.env.REGISTRY;

// return tnpm if is a interior npm
if (isAliNpm(npmName)) return 'https://registry.npm.alibaba-inc.com';

// get registry through npm config
let npmRegistry = spawn.sync('npm', ['config', 'get', 'registry'], { stdio: ['ignore', 'pipe', 'pipe'] });
npmRegistry = npmRegistry.stdout.toString().replace(/\/+(\n?)$/, '');

// return registry
if (isVaildRegistry(npmRegistry)) return npmRegistry;

// default
return 'https://registry.npmjs.com';
}

/**
* verify a registry URL
*
* @param {string} url
*/
function isVaildRegistry(url) {
return /^(https?):\/\/.+$/.test(url);
}

module.exports = {
generateNpmNameByPrefix,
getNpmTime,
getNpmRegistry,
};
8 changes: 7 additions & 1 deletion packages/ice-devtools/utils/template-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,21 @@ function render(options, done) {
const metalsmith = Metalsmith(src);
metalsmith.frontmatter(false);

const realNpmName = kebabCase(npmName || name).replace(/^-/, '');
const data = Object.assign(metalsmith.metadata(), {
name: kebabCase(name).replace(/^-/, ''),
npmName: kebabCase(npmName || name).replace(/^-/, ''),
npmName: realNpmName,
className: uppercamelcase(name),
inPlace: dest === process.cwd(),
noEscape: true,
registry: getNpmRegistry(npmName || name),
categories: {}, // TODO: 已废弃,使用 category,下版本移除
category: '',
// for rax
projectName: realNpmName,
projectAuthor: 'rax',
projectTargets: ['web'],
projectFeatures: [],
...opts,
});
logger.verbose('Metalsmith render options', data);
Expand Down
16 changes: 11 additions & 5 deletions packages/ice-devtools/utils/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ module.exports = (files, data, done) => {
Object.keys(files).forEach((filename) => {
let newFilename = filename;

if (/\.ejs$/.test(newFilename)) {
newFilename = newFilename.replace(/\.ejs$/, '');
files[newFilename] = files[filename];
delete files[filename];
}

/* eslint-disable-next-line no-useless-escape */
if (/[_\.]/.test(filename)) {
if (/[_\.]/.test(newFilename)) {
/* eslint-disable-next-line no-useless-escape */
newFilename = filename.replace(/[_\.]_className__/, data.className);
newFilename = newFilename.replace(/[_\.]_className__/, data.className);
}
// if transformRegexp is passed, filename must match regexp
if (data.transformRegexp && !data.transformRegexp.test(filename)) {
if (data.transformRegexp && !data.transformRegexp.test(newFilename)) {
return;
}
// _gitignore -> .gitignore
// Special logic:_package.json -> package.json
if (filename === '_package.json') {
newFilename = filename.replace(/^_/, '');
newFilename = newFilename.replace(/^_/, '');
} else {
newFilename = filename.replace(/^_/, '.');
newFilename = newFilename.replace(/^_/, '.');
}

if (newFilename !== filename) {
Expand Down
2 changes: 1 addition & 1 deletion packages/ice-react-material-template/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@icedesign/ice-react-material-template",
"version": "0.2.5",
"version": "0.2.6",
"description": "自定义物料源的初始模板",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
"screenshot.png"
],
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/alibaba/ice"
},
"keywords": [
"ice",
"react",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import styles from './index.module.css';

export default function <%= className %>({ value }) {
return (
<div className={styles['<%= name %>']}>
<div className={styles.<%= className %>}>
<%= name %> {value}
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
"prepublishOnly": "npm run build"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/alibaba/ice"
},
"keywords": [
"ice",
"react",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"_stylelintignore",
"_stylelintrc.js",
"ice.config.js",
"screenshot.png"
"screenshot.png",
"jsconfig.json"
],
"scripts": {
"start": "ice-scripts dev",
Expand Down Expand Up @@ -52,9 +53,5 @@
"name": "<%= npmName %>",
"title": "<%= title %>",
"category": "<%= category %>"
},
"repository": {
"type": "git",
"url": "https://github.com/alibaba/ice"
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"baseUrl": ".",
"jsx": "react",
"paths": {
"@/*": ["./src/*"]
}
Expand Down

This file was deleted.

36 changes: 36 additions & 0 deletions packages/ice-react-ts-material-template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# ice-react-ts-material-template

## 简介

用于 ice-devtools init 初始化时的物料仓库模板,基于 react 和 next 组件库

## 使用

```bash
$ npm install ice-devtools -g
$ ice-devtools init
```

#### 区块预览

```
$ cd my-project/blocks/ExampleBlock
$ npm install
$ npm run start
```

#### 布局预览

```
$cd my-project/layouts/ExampleComponent
$ npm install
$ npm run start
```

#### 模板预览

```
$ cd my-project/scaffolds/ExampleScaffold
$ npm install
$ npm run start
```

0 comments on commit 59d1fc2

Please sign in to comment.