Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/thin-hats-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@aiou/neo": minor
"@aiou/preset-aiou": minor
"@aiou/schema": minor
---

list configs
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"@pnpm/logger": "^4.0.0",
"@pnpm/package-store": "^12.1.3",
"cli-columns": "^4.0.0",
"clipboardy": "^3.0.0",
"commander": "8.0.0",
"debug": "4.3.2",
"find-up": "^6.2.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import groupby from 'lodash.groupby'
import countby from 'lodash.countby'
import uniqby from 'lodash.uniqby'

import { CommonOptions, Package } from './interface'
import logger from './utils/logger'
import createStore from './store'
import { CommonOptions, Package } from '../../interface'
import logger, { debug } from '../../utils/logger'
import createStore from '../../store'
import { listConfigs } from './list-configs'

type ListOptions = CommonOptions & {
preset: string[]
Expand All @@ -29,8 +30,14 @@ const colorify = (pkgs: Partial<Package>[], counters: Record<string, number>) =>
* @description List all templates
* @todo list template with detail
*/
export const list = async (params: ListOptions) => {
export const list = async (config: string, params: ListOptions) => {
const store = await createStore(params)
if (config === 'configs') {
debug.list('list config %s', config)
await listConfigs(store)
return
}
// list all packages
const templates = uniqby(
await store.lockFile.readTemplates({ presetNames: params.preset }),
'pref',
Expand Down
51 changes: 51 additions & 0 deletions packages/core/src/commands/list/list-configs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @fileoverview select and copy rc file
*/
import inquirer from 'inquirer'
import InquirerSearchList from 'inquirer-search-list'
import path from 'path'
import tempy from 'tempy'
import fs from 'fs-extra'
import copy from 'clipboardy'

import { AsyncReturnType } from '../../interface'
import type createStore from '../../store'
import logger from '../../utils/logger'

inquirer.registerPrompt('search-list', InquirerSearchList)

export const listConfigs = async (store: AsyncReturnType<typeof createStore>) => {
// read all config files
const configs = await store.lockFile.readConfigs()
if (!configs.length) {
logger.log(` No configs...`)
return
}
const answers = await inquirer.prompt<{ config: string }>([
{
type: 'search-list',
name: 'config',
choices: configs.map((c) => ({
name: c.name,
})),
},
])
const pref = configs.find((choice) => choice.name === answers.config)
if (!pref) {
return
}
const fileResponse = await store.pm.request({ alias: pref.preset, pref: pref.preset })
const files = await fileResponse?.files?.()
if (!files) {
return
}
// import to tempy dir
const dir = tempy.directory()
store.pm.import(dir, files)
// copy it
const filepath = path.join(dir, pref.pref!)
copy.writeSync(fs.readFileSync(filepath).toString())
// log message
console.log()
logger.success(` 🎉 config ${pref?.name} copied, Happy hacking!`)
}
11 changes: 5 additions & 6 deletions packages/core/src/interface/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { ResolveResult } from '@pnpm/package-store'
import type { PresetTemplate } from '@aiou/schema'
import type { PresetTemplate, Preset } from '@aiou/schema'

export type { Config } from '@aiou/schema'

export type CommonOptions = {
storeDir?: string
}

export interface PresetPackage extends PresetTemplate {
export interface Package extends PresetTemplate {
resolvedVia?: ResolveResult['resolvedVia']
}

export interface Package extends PresetPackage {
version: string
pref: string
id: string
Expand All @@ -22,7 +21,7 @@ export interface Package extends PresetPackage {

export type LockFile = {
templates?: Record<string, Package>
presets?: Record<string, { templates: PresetPackage[] }>
presets?: Record<string, Preset>
}

export type AsyncReturnType<T extends (...args: any) => Promise<any>> = T extends (
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/neo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const cli = program

const commands = {
create: async () => await import('./create').then((res) => res.create),
list: async () => await import('./list').then((res) => res.list),
list: async () => await import('./commands/list').then((res) => res.list),
add: async () => await import('./add').then((res) => res.add),
prepack: async () => await import('./prepack').then((res) => res.prepack),
whoami: async () => await import('./whoami').then((res) => res.whoami),
Expand Down Expand Up @@ -51,7 +51,7 @@ cli
.action(handler('create'))

cli
.command('list')
.command('list [configs]')
.description('List all templates')
.alias('l')
.option('--store-dir [storeDir]', 'Set store dir')
Expand Down
14 changes: 13 additions & 1 deletion packages/core/src/store/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path'
import fs from 'fs-extra'

import { LOCK_FILE, STORE_PATH } from '../utils/constants'
import { CommonOptions, LockFile, Package } from '../interface'
import { CommonOptions, LockFile, Package, Config } from '../interface'
import { debug } from '../utils/logger'
import { isMatchPreset } from '../utils'

Expand Down Expand Up @@ -88,6 +88,18 @@ export const createLockFile = ({ lockFilePath }: { lockFilePath: string }) => {
debug.lockfile('templates list %O', allTemplates)
return allTemplates
},
/**
* @description read `neo-lock` preset config files
*/
async readConfigs() {
const lockFile = await this.read()
const presets: LockFile['presets'] = lockFile.presets || {}
return Object.keys(presets).reduce((acc, cur) => {
// inject original preset
const configs = presets[cur]?.configs || []
return acc.concat(configs.map((config) => ({ ...config, preset: cur })))
}, [] as Partial<Config & { preset: string }>[])
},
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/utils/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const debug = {
store: Debug('neo:store'),
add: Debug('neo:cmd:add'),
create: Debug('neo:cmd:create'),
list: Debug('neo:cmd:list'),
}

export default {
Expand Down
12 changes: 12 additions & 0 deletions packages/core/test/cmd-list/list-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { execNeo, mockLockFile, storeDir } from '../helpers'

beforeAll(async () => {
await mockLockFile()
})

describe('command list', () => {
it('should list configs', async () => {
const { stdout } = await execNeo(['list', 'configs', '--store-dir', storeDir])
expect(stdout).toMatchSnapshot()
})
})
33 changes: 33 additions & 0 deletions packages/presets/aiou/assets/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Node.js CI

on:
push:
pull_request:
branches: [master,release]

jobs:
test:
runs-on: ubuntu-latest
name: test

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]

steps:
- name: checkout code repository
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Install pnpm
run: npm i pnpm@latest -g
- name: Install
run: |
pnpm install --frozen-lockfile=false
- name: Test
run: |
pnpm test
6 changes: 6 additions & 0 deletions packages/presets/aiou/index.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"configs": [
{
"name": "Pnpm CI workflow",
"pref": "./assets/workflows/ci.yaml"
}
],
"templates": [
{
"name": "@aiou/ts-lib-template"
Expand Down
4 changes: 4 additions & 0 deletions packages/presets/aiou/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"email": "jiangweixian1994@gmail.com",
"url": "https://twitter.com/jiangweixian"
},
"files": [
"assets",
"index.json"
],
"main": "index.json",
"scripts": {
"build": "echo build",
Expand Down
6 changes: 6 additions & 0 deletions packages/schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ export interface PresetTemplate {
pref: string
}

export type Config = {
pref: string
name: string
}

export type Preset = {
templates: PresetTemplate[]
configs: Config[]
}
28 changes: 28 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.