Skip to content

Commit

Permalink
feat: get default preset from file-system, not cdn (#83)
Browse files Browse the repository at this point in the history
* refactor: get default preset from file-system

* chore(release): publish v0.1.14

* ci: remove cdn sync action
  • Loading branch information
yuzhanglong committed Jun 10, 2021
1 parent 26b64fd commit 02c0854
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 83 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/cdn-reload.yml

This file was deleted.

2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"message": ":bookmark: chore(release): publish %s"
}
},
"version": "0.1.13",
"version": "0.1.14",
"npmClient": "yarn",
"useWorkspaces": true
}
6 changes: 3 additions & 3 deletions packages/serendipity-cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@attachments/serendipity",
"version": "0.1.13",
"version": "0.1.14",
"main": "./lib/serendipity.js",
"bin": {
"serendipity": "./lib/serendipity.js"
Expand All @@ -13,8 +13,8 @@
},
"license": "MIT",
"dependencies": {
"@attachments/serendipity-core": "^0.1.13",
"@attachments/serendipity-public": "^0.1.13",
"@attachments/serendipity-core": "^0.1.14",
"@attachments/serendipity-public": "^0.1.14",
"commander": "^7.0.0"
},
"files": [
Expand Down
8 changes: 4 additions & 4 deletions packages/serendipity-core/__tests__/create.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import { useSerendipityCreate } from '../src'
jest.unmock('execa')


describe('测试 create API', () => {
describe.skip('测试 create API', () => {
const helloPreset = path.resolve(process.cwd(), 'examples', 'test-cases', 'preset-cases', 'hello-world.js')

test.skip('create API 正常初始化,临时预设文件被创建到项目的根目录下', async () => {
test('create API 正常初始化,临时预设文件被创建到项目的根目录下', async () => {
const f = fsMock({})

const c = await useSerendipityCreate({
Expand All @@ -32,7 +32,7 @@ describe('测试 create API', () => {
expect(fs.existsSync(f.resolve('foo', 'package.json'))).toBeTruthy()
})

test.skip('如果目标路径已经存在,我们不会再其下创建一个项目,而是抛出异常', async () => {
test('如果目标路径已经存在,我们不会再其下创建一个项目,而是抛出异常', async () => {
const f = fsMock({
foo: {
'a.out': 'aaa'
Expand All @@ -53,7 +53,7 @@ describe('测试 create API', () => {
}
})

test.skip('测试 construction 模式,质询、构建阶段应该被正常执行', async () => {
test('测试 construction 模式,质询、构建阶段应该被正常执行', async () => {
const f = fsMock({})

const c = await useSerendipityCreate({
Expand Down
21 changes: 17 additions & 4 deletions packages/serendipity-core/__tests__/preset-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ describe('preset Manager 模块测试', () => {
// @ts-ignore
await PresetManager.createPresetManagerByRemotePath(666, 666)
} catch (e) {
expect(e.message).toStrictEqual('url.startsWith is not a function')
expect(e.message).toStrictEqual('target.startsWith is not a function')
}

try {
Expand Down Expand Up @@ -168,7 +168,7 @@ describe('preset Manager 模块测试', () => {
})
})

test('当 preset 传入的不是一个本地路径,也不是一个 http URL 我们会从 GitHub 仓库的默认 preset 获取', async () => {
test('当 preset 传入的不是一个本地路径,我们要求用户传入回调函数来获取', async () => {
const f = fsMock({})
mock.onGet(`${PRESET_CDN_BASE_URL}/react.js`).reply(200, 'const path = require(\'path\')\n' +
'\n' +
Expand All @@ -182,7 +182,11 @@ describe('preset Manager 模块测试', () => {
' }\n' +
' ]\n' +
'}')
const pm = await PresetManager.createPresetManager('react', f.path)

const pm = await PresetManager.createPresetManager('react', f.path, async (target) => {
return await PresetManager.createPresetManagerByRemotePath(f.path, `${PRESET_CDN_BASE_URL}/${target}.js`)
})

expect(pm.getPreset()).toStrictEqual({
'initialDir': true,
'initialDirDefaultName': 'my-project',
Expand All @@ -197,5 +201,14 @@ describe('preset Manager 模块测试', () => {
})
f.clear()
})
})

test('当 preset 传入的不是一个本地路径,且用户没有传入回调函数,我们会抛出一个异常', async () => {
const f = fsMock({})

try {
await PresetManager.createPresetManager('react', f.path)
} catch (e) {
expect(e.message).toStrictEqual('you should pass callback for not-remote-path and not-local-path preset!')
}
})
})
10 changes: 10 additions & 0 deletions packages/serendipity-core/__tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as path from 'path'
import { getPresetPathByName } from '../src/utils'

describe('工具函数测试', () => {
test('test getPresetPathByName()', () => {
const p = getPresetPathByName('react')
const casePath = path.resolve(process.cwd(), 'packages', 'serendipity-core', 'presets', 'react.js')
expect(p).toStrictEqual(casePath)
})
})
7 changes: 4 additions & 3 deletions packages/serendipity-core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@attachments/serendipity-core",
"version": "0.1.13",
"version": "0.1.14",
"description": "> TODO: description",
"author": "yuzhanglong <yuzl1123@163.com>",
"homepage": "https://github.com/yuzhanglong/serendipity/tree/master/packages/serendipity-core#readme",
Expand All @@ -15,13 +15,14 @@
"clean": "rimraf lib esm"
},
"dependencies": {
"@attachments/serendipity-public": "^0.1.13",
"@attachments/serendipity-public": "^0.1.14",
"tapable": "^2.2.0"
},
"files": [
"esm",
"lib",
"src"
"src",
"presets"
],
"gitHead": "49d6e80c784169331de048713142cc8b19dac1ff"
}
File renamed without changes.
File renamed without changes.
11 changes: 9 additions & 2 deletions packages/serendipity-core/src/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
*/
import * as path from 'path'
import * as fs from 'fs-extra'
import { logger } from '@attachments/serendipity-public'
import { isLocalPath, isRemotePath, logger } from '@attachments/serendipity-public'
import { PresetManager } from './preset-manager'
import createCoreManagerHooks from './hooks/core-manager-hooks'
import { ConstructionManager } from './construction-manager'
import { CreateOptions } from './types'
import { getPresetPathByName } from './utils'


/**
Expand Down Expand Up @@ -43,8 +44,14 @@ export async function useSerendipityCreate(createOptions: CreateOptions) {
// 如果 projectDir 不存在,创建之
await fs.ensureDir(projectDir)

let tmpPresetPath = presetPath

if (!isRemotePath(tmpPresetPath) && !isLocalPath(tmpPresetPath)) {
tmpPresetPath = getPresetPathByName(presetPath)
}

// 初始化预设管理器
const pm = await PresetManager.createPresetManager(presetPath, projectDir)
const pm = await PresetManager.createPresetManager(tmpPresetPath, projectDir)

const execute = async () => {
// [hooks] -- beforePluginInstall 在 plugin 安装前做些什么
Expand Down
42 changes: 16 additions & 26 deletions packages/serendipity-core/src/preset-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import axios from 'axios'
import {
logger, writeFilePromise,
DEFAULT_PRESET_NAME,
BaseObject, PRESET_CDN_BASE_URL
BaseObject, isRemotePath, isLocalPath
} from '@attachments/serendipity-public'
import { DEFAULT_PRESET } from './common'
import { SerendipityPreset } from './types'
Expand Down Expand Up @@ -45,8 +45,7 @@ export class PresetManager {
*/
public static async createPresetManagerByRemotePath(basePath: string, url: string) {
// 判断是否为远程路径
const isRemotePath = url.startsWith('http://') || url.startsWith('https://')
if (!isRemotePath) {
if (!isRemotePath(url)) {
throw new Error('不合法的 preset 路径!')
}

Expand All @@ -72,8 +71,7 @@ export class PresetManager {
* @param localPath preset 本地路径
*/
public static async createPresetManagerByLocalPath(localPath: string) {
const isLocalPath = localPath.startsWith('/') || (localPath.match(/[a-zA-Z]:(\\\\)|(\/\/)|(\\)/) !== null)
if (!isLocalPath) {
if (!isLocalPath(localPath)) {
throw new Error('不合法的本地 preset 路径!')
}
const res = require(localPath)
Expand All @@ -82,37 +80,29 @@ export class PresetManager {
}

/**
* 根据本地路径获取 presetManager
* 路径获取 presetManager
* - 当目的路径为远程路径(http protocol) 或者本地路径(file protocol)时,分别调用相应工厂函数
* - 如果目的路径不是上面两种情况,我们要求用户传入一个回调函数来处理!
*
* @author yuzhanglong
* @date 2021-6-3 00:32:34
* @param target 目标 url
* @param tmpPath preset 临时保存路径
* @param callback 回调函数,见上面的描述
*/
public static async createPresetManager(target: string, tmpPath?: string) {
// 判断是否为远程路径
const isRemotePath = target.startsWith('http://') || target.startsWith('https://')
const isLocalPath = target.startsWith('/') || (target.match(/[a-zA-Z]:(\\\\)|(\/\/)|(\\)/) !== null)
if (!isRemotePath && !isLocalPath) {
return await PresetManager.createPresetByName(tmpPath, target)
public static async createPresetManager(target: string, tmpPath?: string, callback?: (path: string) => any) {
if (!isRemotePath(target) && !isLocalPath(target)) {
if (!callback) {
throw new Error('you should pass callback for not-remote-path and not-local-path preset!')
}
return await callback(target)
}
if (isRemotePath) {

if (isRemotePath(target)) {
return await PresetManager.createPresetManagerByRemotePath(tmpPath, target)
}
return await PresetManager.createPresetManagerByLocalPath(target)
}

/**
* 根据 preset 名称获取 presetManager,我们会从 GitHub 仓库的默认 preset 目录下获取
*
* @author yuzhanglong
* @date 2021-5-28 22:43:58
* @param basePath 基本路径
* @param name preset 名称
*/
public static async createPresetByName(basePath: string, name: string) {
const remotePath = `${PRESET_CDN_BASE_URL}/${name}.js`
return PresetManager.createPresetManagerByRemotePath(basePath, remotePath)
return await PresetManager.createPresetManagerByLocalPath(target)
}

/**
Expand Down
9 changes: 9 additions & 0 deletions packages/serendipity-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,12 @@ export const getBasePackageJsonContent = (name?: string) => {
'license': 'MIT'
}
}


export function getPresetPathByName(name: string) {
// 如果没有后缀名,我们需要加上
if (!name.endsWith('.js')) {
name = name + '.js'
}
return path.resolve(__dirname, '../presets', name)
}
8 changes: 4 additions & 4 deletions packages/serendipity-plugin-babel/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@attachments/serendipity-plugin-babel",
"version": "0.1.13",
"version": "0.1.14",
"main": "./lib/index.js",
"license": "MIT",
"scripts": {
Expand All @@ -10,9 +10,9 @@
"clean": "rimraf lib esm"
},
"dependencies": {
"@attachments/serendipity-core": "^0.1.13",
"@attachments/serendipity-plugin-react": "^0.1.13",
"@attachments/serendipity-public": "^0.1.13",
"@attachments/serendipity-core": "^0.1.14",
"@attachments/serendipity-plugin-react": "^0.1.14",
"@attachments/serendipity-public": "^0.1.14",
"@babel/core": "^7.12.10",
"@babel/preset-env": "^7.12.13",
"@babel/preset-react": "^7.12.10",
Expand Down
8 changes: 4 additions & 4 deletions packages/serendipity-plugin-eslint/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@attachments/serendipity-plugin-eslint",
"version": "0.1.13",
"version": "0.1.14",
"main": "lib/index.js",
"scripts": {
"start": "tsc -w",
Expand All @@ -13,9 +13,9 @@
"webpack": "^5.21.2"
},
"dependencies": {
"@attachments/serendipity-core": "^0.1.13",
"@attachments/serendipity-plugin-react": "^0.1.13",
"@attachments/serendipity-public": "^0.1.13",
"@attachments/serendipity-core": "^0.1.14",
"@attachments/serendipity-plugin-react": "^0.1.14",
"@attachments/serendipity-public": "^0.1.14",
"eslint-webpack-plugin": "^2.4.3"
},
"files": [
Expand Down
4 changes: 2 additions & 2 deletions packages/serendipity-plugin-init/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@attachments/serendipity-plugin-init",
"version": "0.1.13",
"version": "0.1.14",
"author": "yuzhanglong <yuzl1123@163.com>",
"license": "MIT",
"scripts": {
Expand All @@ -15,7 +15,7 @@
"url": "git+https://github.com/yuzhanglong/serendipity.git"
},
"dependencies": {
"@attachments/serendipity-core": "^0.1.13"
"@attachments/serendipity-core": "^0.1.14"
},
"bugs": {
"url": "https://github.com/yuzhanglong/serendipity/issues"
Expand Down
8 changes: 4 additions & 4 deletions packages/serendipity-plugin-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@attachments/serendipity-plugin-react",
"version": "0.1.13",
"version": "0.1.14",
"main": "lib/index.js",
"module": "esm/index.js",
"license": "MIT",
Expand All @@ -11,9 +11,9 @@
"clean": "rimraf lib esm"
},
"dependencies": {
"@attachments/serendipity-core": "^0.1.13",
"@attachments/serendipity-public": "^0.1.13",
"@attachments/serendipity-webpack-plugin": "^0.1.13",
"@attachments/serendipity-core": "^0.1.14",
"@attachments/serendipity-public": "^0.1.14",
"@attachments/serendipity-webpack-plugin": "^0.1.14",
"@pmmmwh/react-refresh-webpack-plugin": "^0.4.3",
"@types/webpack-dev-server": "^3.11.1",
"commander": "^7.0.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/serendipity-plugin-typescript/package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@attachments/serendipity-plugin-typescript",
"version": "0.1.13",
"version": "0.1.14",
"main": "lib/index.js",
"license": "MIT",
"dependencies": {
"@attachments/serendipity-core": "^0.1.13",
"@attachments/serendipity-public": "^0.1.13"
"@attachments/serendipity-core": "^0.1.14",
"@attachments/serendipity-public": "^0.1.14"
},
"scripts": {
"start": "tsc -w",
Expand Down
2 changes: 1 addition & 1 deletion packages/serendipity-public/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@attachments/serendipity-public",
"version": "0.1.13",
"version": "0.1.14",
"main": "lib/index.js",
"module": "esm/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions packages/serendipity-public/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Email: yuzl1123@163.com
*/

export { isRemotePath, isLocalPath } from './utils/paths'
export { default as webpackMerge } from 'webpack-merge'
export { serendipityEnv } from './utils/env'
export { runCommand } from './utils/run-command'
Expand Down
Loading

0 comments on commit 02c0854

Please sign in to comment.