Skip to content

Commit

Permalink
chore: transfer from aspidajs/aspida
Browse files Browse the repository at this point in the history
  • Loading branch information
solufa committed May 28, 2020
0 parents commit b296f05
Show file tree
Hide file tree
Showing 30 changed files with 6,739 additions and 0 deletions.
65 changes: 65 additions & 0 deletions $path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* eslint-disable */
interface Query0 {
id: number
values: {
type: string
index: number
}[]
}

type Query1 = {
hoge: number // comment
fuga: string[]
/*
comment
*/
}

const encode = (str: Parameters<typeof encodeURIComponent>[0]) =>
encodeURIComponent(str).replace(
/[!'()~]|%20|%00/g,
match =>
(({
'!': '%21',
"'": '%27',
'(': '%28',
')': '%29',
'~': '%7E',
'%20': '+',
'%00': '\x00'
} as Record<string, string>)[match])
)

const dataToURLString = (data: Record<string, any>) =>
Object.keys(data)
.map(key =>
Array.isArray(data[key])
? data[key].map((v: string) => `${encode(key)}=${encode(v)}`).join('&')
: `${encode(key)}=${encode(data[key])}`
)
.join('&')

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

return {
_contentId: (val0: number | string) => ({
$get: (query: Query0) => `${prefix}/${val0}/?${dataToURLString(query)}`
}),
users: {
_userId: (val1: number | string) => ({
_testVal: (val2: number | string) => ({
$get: () => `${prefix}/users/${val1}/${val2}/`
}),
test: {
$get: (query: Query1) => `${prefix}/users/${val1}/test/?${dataToURLString(query)}`
},
$get: () => `${prefix}/users/${val1}/`
})
},
$get: () => `${prefix}/`
}
}

export type PathInstance = ReturnType<typeof path>
export default path
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 m-mitsuhide <m.mitsuhide@amatelus.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
107 changes: 107 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# pathpida

<br />
<br />
<br />
<br />
<br />
<div align="center">
<img src="https://aspidajs.github.io/pathpida/logos/svg/black.svg" alt="pathpida" title="pathpida" width="700" />
</div>
<br />
<br />
<br />
<div align="center">
<a href="https://www.npmjs.com/package/pathpida">
<img src="https://img.shields.io/npm/v/pathpida" alt="npm version" />
</a>
<a href="https://codecov.io/gh/aspidajs/pathpida">
<img src="https://img.shields.io/codecov/c/github/aspidajs/pathpida.svg" alt="Codecov" />
</a>
<a href="https://dependabot.com">
<img src="https://api.dependabot.com/badges/status?host=github&repo=aspidajs/pathpida" alt="Dependabot Status" />
</a>
<a href="https://github.com/aspidajs/pathpida/LICENSE">
<img src="https://img.shields.io/npm/l/pathpida" alt="License" />
</a>
</div>
<br />
<p align="center">TypeScript friendly path generator for Next.js and Nuxt.js.</p>
<br />
<br />

## Getting Started

### Installation

- Using [npm](https://www.npmjs.com/):

```sh
$ npm install pathpida --save-dev
```

- Using [Yarn](https://yarnpkg.com/):

```sh
$ yarn add pathpida --dev
```

### Make HTTP request from application

`pathpida.config.js`

```js
module.exports = {
input: "pages",
output: "types",
baseURL: "https://example.com/api",
trailingSlash: false
}
```

`package.json`

```json
{
"scripts": {
"build:pathpida": "pathpida --build"
}
}
```

`pages/users/[userId].tsx`

```ts
import React from "react"
import $path from "../types/$path"
export type Query = {
hoge: string
}
export default () => <div>user info</div>
```

`tarminal`

```sh
$ npm run build:pathpida
# types/$path.ts was built successfully.
```

`pages/index.tsx`

```ts
import React from "react"
import $path from "../types/$path"
export type Query = {
pageid: number
}
export default () => <a href={$path().users._userId(0).$get({ hoge: "fuga" })}>Link to user page</a>
```

## License

pathpida is licensed under a [MIT License](https://github.com/aspidajs/pathpida/LICENSE).
20 changes: 20 additions & 0 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from 'fs'
import build from '../src/buildTemplate'
import getConfig from '../src/getConfig'

const basePath = 'packages/pathpida'

describe('cli test', () => {
test('main', () => {
const { input, output, baseURL, trailingSlash } = getConfig(`${basePath}/pathpida.config.js`)[0]
const inputDir = `${basePath}/${input}`
const outputDir = `${basePath}/${output}`

const resultFilePath = `${outputDir}$path.ts`
const result = fs.readFileSync(resultFilePath, 'utf8')
const { filePath, text } = build({ input: inputDir, output: outputDir, baseURL, trailingSlash })

expect(filePath).toBe(resultFilePath)
expect(text).toBe(result)
})
})
2 changes: 2 additions & 0 deletions bin/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env node
require('../dist/cli').run(process.argv.slice(2))
1 change: 1 addition & 0 deletions docs/logos/svg/black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions docs/logos/svg/white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
65 changes: 65 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "pathpida",
"version": "0.5.0",
"description": "TypeScript friendly path generator for Next.js or Nuxt.js",
"author": "m-mitsuhide <m.mitsuhide@amatelus.com>",
"license": "MIT",
"bin": "bin/index.js",
"scripts": {
"dev": "npm run build && node bin/index.js --build",
"build": "npm run rimraf && tsc",
"rimraf": "node -e \"require('fs').rmdirSync('dist', { recursive: true })\"",
"release": "standard-version --skip.tag",
"release:major": "npm run release -- --release-as major",
"release:minor": "npm run release -- --release-as minor",
"release:patch": "npm run release -- --release-as patch",
"lint": "eslint --ext .js,.ts --ignore-path .gitignore .",
"lint:fix": "npm run lint -- --fix",
"test": "jest",
"test:watch": "npm test -- --watch",
"typecheck": "tsc --noEmit"
},
"homepage": "https://github.com/aspidajs/pathpida#readme",
"repository": {
"type": "git",
"url": "git+https://github.com/aspidajs/pathpida.git"
},
"bugs": {
"url": "https://github.com/aspidajs/pathpida/issues"
},
"files": [
"dist"
],
"keywords": [
"typescript",
"path"
],
"dependencies": {
"chokidar": "^3.4.0",
"minimist": "^1.2.5"
},
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@types/jest": "^25.2.3",
"@types/minimist": "^1.2.0",
"@typescript-eslint/eslint-plugin": "2.34.0",
"@typescript-eslint/parser": "^2.34.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.11.0",
"eslint-config-standard": "^14.1.1",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-jest": "^23.13.2",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.1.3",
"eslint-plugin-promise": "^4.2.1",
"eslint-plugin-standard": "^4.0.1",
"husky": "^4.2.5",
"jest": "^25.5.4",
"lint-staged": "^10.2.6",
"prettier": "2.0.5",
"standard-version": "^8.0.0",
"ts-jest": "^25.5.1",
"typescript": "^3.9.3"
}
}
6 changes: 6 additions & 0 deletions pathpida.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
input: 'samples',
output: '',
baseURL: 'https://example.com/api',
trailingSlash: true
}
7 changes: 7 additions & 0 deletions samples/_contentId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface Query {
id: number
values: {
type: string
index: number
}[]
}
Empty file added samples/index.tsx
Empty file.
Empty file added samples/users/_userId.ts
Empty file.
Empty file.
Empty file added samples/users/_userId/index.js
Empty file.
7 changes: 7 additions & 0 deletions samples/users/_userId/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type Query = {
hoge: number // comment
fuga: string[]
/*
comment
*/
}
28 changes: 28 additions & 0 deletions src/buildTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import path from 'path'
import { Config } from './getConfig'
import createTemplateValues from './createTemplateValues'
import dataToURLString from './dataToURLString'

export type Template = {
text: string
filePath: string
}

export default ({ input, baseURL, output, trailingSlash }: Config): Template => {
const { api, imports } = createTemplateValues(input, trailingSlash)

const text = `/* eslint-disable */
${imports.map(i => i.replace(input, '.')).join('\n')}
${api.includes('dataToURLString') ? dataToURLString : ''}
const path = (baseURL?: string) => {
const prefix = (baseURL === undefined ? '${baseURL}' : baseURL).replace(/\\/$/, '')
return ${api}
}
export type PathInstance = ReturnType<typeof path>
export default path
`

return { text, filePath: path.posix.join(output, '$path.ts') }
}
51 changes: 51 additions & 0 deletions src/cli/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Config } from '../getConfig'
import build, { Template } from '../buildTemplate'
import { Command } from './command'

export class CommandToBuild implements Command {
static getFactory(configs: Config[], io: BuildIO) {
return {
create(command: BuildCommand): Command {
return new CommandToBuild(command, configs, io)
}
}
}

// eslint-disable-next-line no-useless-constructor
private constructor(
private readonly command: BuildCommand,
private readonly configs: Config[],
private readonly io: BuildIO
) {}

exec() {
this.configs.forEach(config => {
this.command.run(config, this.io)
})
}
}

type BuildCommand = {
run(config: Config, io: BuildIO): void
}

export type BuildIO = {
write(template: Template): void
watch(input: string, callback: () => void): void
}

export class Build implements BuildCommand {
run(config: Config, io: BuildIO) {
io.write(build(config))
}
}

export class Watch implements BuildCommand {
// eslint-disable-next-line no-useless-constructor
constructor(private readonly build = new Build()) {}

run(config: Config, io: BuildIO) {
this.build.run(config, io)
io.watch(config.input, () => this.build.run(config, io))
}
}
Loading

0 comments on commit b296f05

Please sign in to comment.