Skip to content

Commit

Permalink
feat: add aspida.config.js
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed Jan 9, 2020
1 parent 53b2184 commit dc97979
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 45 deletions.
5 changes: 3 additions & 2 deletions package.json
Expand Up @@ -4,15 +4,15 @@
"packages/*"
],
"scripts": {
"build:aspida": "yarn workspace aspida build",
"dev:aspida": "yarn build:aspida && cd packages/aspida && node bin/index.js --build",
"build:aspida": "cd packages/aspida && rimraf dist && tsc --project tsconfig.build.json",
"build:aspida-axios": "yarn workspace @aspida/axios build",
"build:aspida-fetch": "yarn workspace @aspida/fetch build",
"build:aspida-ky": "yarn workspace @aspida/ky build",
"build:mock": "yarn workspace axios-mock-server build",
"release": "standard-version --skip.tag",
"lint": "eslint --ext .js,.ts --ignore-path .gitignore .",
"lint:fix": "npm run lint -- --fix",
"snapshot:aspida": "ts-node packages/aspida/__tests__/utils/updateSnapshot",
"snapshot:mock": "ts-node packages/axios-mock-server/__tests__/utils/updateSnapshot",
"test": "jest",
"test:watch": "npm test -- --watch",
Expand All @@ -23,6 +23,7 @@
"@commitlint/config-conventional": "^8.3.4",
"@types/jest": "^24.0.25",
"@types/minimist": "^1.2.0",
"@types/rimraf": "^2.0.3",
"@typescript-eslint/eslint-plugin": "2.15.0",
"@typescript-eslint/parser": "^2.15.0",
"axios": "^0.19.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/aspida/__tests__/apis/$api.ts
Expand Up @@ -12,7 +12,7 @@ import { Methods as Methods6 } from './v1.1/users/_userId@number'
import { Methods as Methods7 } from './v2.0/index'

const api = (client: AspidaClient, baseURL?: string) => {
const prefix = (baseURL === undefined ? 'https://example.com' : baseURL).replace(/\/$/, '')
const prefix = (baseURL === undefined ? '' : baseURL).replace(/\/$/, '')

return {
v1_1: {
Expand Down
3 changes: 1 addition & 2 deletions packages/aspida/__tests__/index.spec.ts
Expand Up @@ -3,7 +3,6 @@ import build from '~/aspida/src/buildTemplate'

describe('cli test', () => {
test('main', () => {
const baseurl = 'https://example.com'
const paths = [
{
input: 'packages/aspida/__tests__/apis',
Expand All @@ -26,7 +25,7 @@ describe('cli test', () => {
paths.forEach(({ input, resultDirPath }) => {
const resultFilePath = `${resultDirPath}/$api.ts`
const result = fs.readFileSync(resultFilePath, 'utf8')
const { text, filePath } = build(input, baseurl)
const { text, filePath } = build(input)

expect(text).toBe(result)
expect(filePath).toBe(resultFilePath)
Expand Down
7 changes: 0 additions & 7 deletions packages/aspida/__tests__/utils/updateSnapshot.ts

This file was deleted.

3 changes: 3 additions & 0 deletions packages/aspida/aspida.config.js
@@ -0,0 +1,3 @@
module.exports = {
aspida: { input: '__tests__/apis' }
}
3 changes: 0 additions & 3 deletions packages/aspida/package.json
Expand Up @@ -23,9 +23,6 @@
"ajax",
"promise"
],
"scripts": {
"build": "rimraf dist && tsc --project tsconfig.build.json"
},
"dependencies": {
"chokidar": "^3.3.1",
"minimist": "^1.2.0",
Expand Down
7 changes: 2 additions & 5 deletions packages/aspida/src/cli.ts
@@ -1,7 +1,6 @@
import fs from 'fs'
import minimist from 'minimist'
import getConfig, { Config } from './getConfig'
import read from './getInputs'
import write from './writeRouteFile'
import watch from './watchInputDir'
import { Build, Watch, CommandToBuild } from './cli/build'
Expand All @@ -15,12 +14,10 @@ const options: minimist.Opts = {

const getBuildCommandFactory = (config: Config) =>
CommandToBuild.getFactory(config, {
read,
write,
watch,
remove(filePath: string, callback: () => void) {
fs.unlink(filePath, callback)
}
read: ({ input }) => (Array.isArray(input) ? input : [input]),
remove: (filePath: string, callback: () => void) => fs.unlink(filePath, callback)
})

export const run = (args: string[]) => {
Expand Down
13 changes: 4 additions & 9 deletions packages/aspida/src/cli/build.ts
Expand Up @@ -20,7 +20,7 @@ export class CommandToBuild implements Command {
) {}

exec() {
this.io.read(this.config.input).forEach(input => {
this.io.read(this.config).forEach(input => {
this.command.run(input, this.io)
})
}
Expand All @@ -31,7 +31,7 @@ interface BuildCommand {
}

export interface BuildIO {
read(input?: string | string[]): string[]
read(config: Config): string[]
write(template: Template): void
remove(filePath: string, callback: () => void): void
watch(input: string, callback: () => void): void
Expand All @@ -41,7 +41,7 @@ export class Build implements BuildCommand {
run(input: string, io: BuildIO): void {
const template = build(input)

io.write(template)
io.remove(template.filePath, () => io.write(template))
}
}

Expand All @@ -51,11 +51,6 @@ export class Watch implements BuildCommand {

run(input: string, io: BuildIO): void {
this.build.run(input, io)

io.watch(input, () => {
const result = build(input)

io.remove(result.filePath, () => io.write(result))
})
io.watch(input, () => this.build.run(input, io))
}
}
17 changes: 7 additions & 10 deletions packages/aspida/src/getConfig.ts
@@ -1,14 +1,11 @@
import fs from 'fs'
import path from 'path'

export type Config = {
input?: string | string[]
}
export type Config = { input: string | string[] }

export const apiFileRegExp = /\/\$[^/]+\.(js|ts)$/
const defaultConfig: Config = { input: 'apis' }

export const defaultConfig = {
input: 'apis'
}

export default (rcFilePath = '.aspidarc'): Config =>
fs.existsSync(rcFilePath) ? JSON.parse(fs.readFileSync(rcFilePath, 'utf8')) : {}
export default (configPath = 'aspida.config.js'): Config =>
(fs.existsSync(configPath) &&
require(path.join(process.cwd(), configPath))[require('../package.json').name]) ||
defaultConfig
4 changes: 0 additions & 4 deletions packages/aspida/src/getInputs.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/aspida/src/watchInputDir.ts
@@ -1,6 +1,5 @@
import chokidar from 'chokidar'
import { apiFileRegExp } from './getConfig'

export default (input: string, callback: (...args: any) => void) => {
chokidar.watch(input, { ignoreInitial: true, ignored: apiFileRegExp }).on('all', callback)
chokidar.watch(input, { ignoreInitial: true, ignored: /\/\$[^/]+\.(js|ts)$/ }).on('all', callback)
}

0 comments on commit dc97979

Please sign in to comment.