Skip to content

Commit

Permalink
feat: use fast-glob instead of vinyl-fs
Browse files Browse the repository at this point in the history
  • Loading branch information
ambar committed Oct 7, 2021
1 parent 22d4732 commit bb49881
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 539 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 25,7 @@ jobs:
- name: test
run: |
yarn
yarn build
yarn jest --coverage
- name: report
uses: coverallsapp/[email protected]
Expand Down
4 changes: 1 addition & 3 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 2,6 @@ nodeLinker: node-modules

plugins:
- path: .yarn/plugins/plugin-envs.cjs
spec: "https://raw.githubusercontent.com/Ayc0/yarn-plugin-envs/v0.0.3/src/index.js"
- path: .yarn/plugins/@yarnpkg/plugin-postinstall-dev.cjs
spec: "https://raw.githubusercontent.com/sachinraja/yarn-plugin-postinstall-dev/main/bundles/@yarnpkg/plugin-postinstall-dev.js"
spec: 'https://raw.githubusercontent.com/Ayc0/yarn-plugin-envs/v0.0.3/src/index.js'

yarnPath: .yarn/releases/yarn-3.0.2.cjs
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,7 @@ Use stream API to scaffold projects or files.

## Features

- [vinyl-fs](https://github.com/gulpjs/vinyl-fs) based, [Gulp](https://github.com/gulpjs/gulp)-compatible stream API
- Simple, easy to use
- Lightweight, single file bundled (<50K gzip size), no need to install globally, just run `npx`
- Automatically rename `gitignore` to `.gitignore`, due to [npm/issues/1862](https://github.com/npm/npm/issues/1862)
- Automatically set `name` field in `package.json`
Expand Down
29 changes: 23 additions & 6 deletions create-gogen/yarn.lock
Original file line number Diff line number Diff line change
@@ -1,8 1,25 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 4
cacheKey: 8

gogen@^0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/gogen/-/gogen-0.0.1.tgz#296301c90ae933108400dc6939f8a38098b6a38b"
integrity sha512-395vZjcpMB6T3 vOphiKZiEhiYs9Y8/Y350P0D1OPR4DM6kbsRwypv2j0b4KemrceFxxPdFKUSoWkAr XJRjvg==
"create-gogen@workspace:.":
version: 0.0.0-use.local
resolution: "create-gogen@workspace:."
dependencies:
gogen: ^1.1.1
bin:
create-gogen: cli.js
languageName: unknown
linkType: soft

"gogen@npm:^1.1.1":
version: 1.1.1
resolution: "gogen@npm:1.1.1"
bin:
gogen: cli.js
checksum: 2dc26f3bc7fc5649d5406b0101e8dd8209d178fd17efca5758ee8279592bd3455ae64d9ca1bba2b73a8fcbfc8511a7bd41100bfc098dad607836a04fe435c744
languageName: node
linkType: hard
4 changes: 4 additions & 0 deletions gogen/cli.js
Original file line number Diff line number Diff line change
@@ -1,3 1,7 @@
#!/usr/bin/env node

require('./dist').run(process.argv.slice(2))

// TODO: ESM
// import {run} from './dist/index.js'
// run(process.argv.slice(2))
57 changes: 45 additions & 12 deletions gogen/lib/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 1,15 @@
import fs from 'fs'
import {promises as fsp} from 'fs'
import path from 'path'
import stream from 'stream'
import {promisify} from 'util'
import * as vfs from 'vinyl-fs'
import stream from 'stream'
import through2 from 'through2'
import {VFile} from './vfile'
import * as fg from 'fast-glob'
import globParent from 'glob-parent'
import colors from 'kleur'
import prompts from 'prompts'
import {ParsedArgv} from './types'
import boolify from './utils/boolify'
import shell from './utils/shell'
import canUseYarn from './utils/canUseYarn'
import createTempDir from './utils/createTempDir'
Expand Down Expand Up @@ -50,7 54,7 @@ const downloadFromNpmPackage = async (packagePath: string) => {
await npmInit(tempDir)
await install([packagePath], {cwd: tempDir, silent: true})
const pkg = JSON.parse(
fs.readFileSync(path.resolve(tempDir, 'package.json')).toString()
(await fsp.readFile(path.resolve(tempDir, 'package.json'))).toString()
)
const depName = Object.keys(pkg.dependencies)[0]
return path.resolve(tempDir, 'node_modules', depName)
Expand All @@ -71,6 75,30 @@ const downloadFromGitRepo = async (repoPath: any) => {
return tempDir
}

let defaultIgnore = ['**/node_modules/**']
async function* globFiles(
globs: string[],
{cwd = process.cwd()}: fg.Options = {}
) {
for (let glob of Array.isArray(globs) ? globs : [globs]) {
for await (const name of fg.stream(glob, {
dot: true,
ignore: defaultIgnore,
cwd,
})) {
let filename = path.resolve(cwd, name as string)
let file = new VFile()
Object.assign(file, {
contents: await fsp.readFile(filename),
cwd,
base: path.resolve(cwd, globParent(glob)),
path: filename,
})
yield file
}
}
}

export const loadGenerator = async (argv: ParsedArgv, {mock}: any = {}) => {
const [generator, directory] = argv._

Expand Down Expand Up @@ -102,8 130,6 @@ export const loadGenerator = async (argv: ParsedArgv, {mock}: any = {}) => {
// if (!fs.existsSync(destPath)) { fs.mkdirSync(destPath) }
// process.chdir(destPath)

const defaultIgnore = ['!**/node_modules', '!**/node_modules/**']

// non-stream API
let extra = {
install: partialOptions(install, {cwd: destPath}) as typeof install,
Expand All @@ -112,13 138,20 @@ export const loadGenerator = async (argv: ParsedArgv, {mock}: any = {}) => {
}

const api = {
src: (globs: string[], options: vfs.SrcOptions) =>
vfs
.src(defaultIgnore.concat(globs), {cwd: srcPath, dot: true, ...options})
src: (globs: string[]) =>
stream.Readable.from(globFiles(globs, {cwd: srcPath}))
.pipe(dotgitignore())
.pipe(packages({name})),
dest: (folder = destPath, options?: vfs.DestOptions) =>
vfs.dest(folder, {cwd: srcPath, ...options}),
dest: (folder = destPath) =>
through2.obj(async (file: VFile, enc: any, next: any) => {
let outBase = path.resolve(srcPath, folder)
let outPath = path.resolve(outBase, file.relative)
file.base = outBase
file.path = outPath
await fsp.mkdir(file.dirname, {recursive: true})
await fsp.writeFile(file.path, file.contents as Buffer)
next(null, file)
}),
// Node v15 has native support (`import {pipeline} from 'stream/promises'`)
pipeline: promisify(stream.pipeline),
packages,
Expand All @@ -143,7 176,7 @@ export const loadGenerator = async (argv: ParsedArgv, {mock}: any = {}) => {

console.info(`Creating ${colors.green(name)}...`)
const rcFile = path.resolve(srcPath, '.gogenrc.js')
const fn = fs.existsSync(rcFile) ? require(rcFile) : defaultRc
const fn = (await boolify(fsp.stat(rcFile))) ? require(rcFile) : defaultRc
return [fn, api, context] as const
}

Expand Down
83 changes: 83 additions & 0 deletions gogen/lib/vfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,83 @@
/// <reference types="node" />
import path from 'path'
import assert from 'assert'

type Content = Buffer | NodeJS.ReadableStream | null

const assertString = (v: string) => {
assert.ok(typeof v === 'string')
}

const assertNoneEmptyString = (v: string) => {
assertString(v)
assert.ok(v)
}

/**
* Virtual File
*
* Like simplified vinyl, no dependencies.
*/
export class VFile {
#base: string = ''
#contents: Content = null
#history: string[] = []

get contents() {
return this.#contents
}
set contents(v: Content) {
this.#contents = v
}

get path() {
return this.#history[this.#history.length - 1]
}
set path(v: string) {
assertString(v)
this.#history.push(v)
}

get dirname() {
return path.dirname(this.path)
}
set dirname(v: string) {
assert.ok(this.path)
this.path = path.join(v, this.basename)
}

get basename() {
assert.ok(this.path)
return path.basename(this.path)
}
set basename(v: string) {
assert.ok(this.path)
this.path = path.join(this.dirname, v)
}

get extname() {
assert.ok(this.path)
return path.extname(this.path)
}
set extname(v: string) {
const {name} = path.parse(this.basename)
this.basename = path.format({name, ext: v})
}

get base() {
return this.#base
}
set base(v: string) {
assertNoneEmptyString(v)
this.#base = v
}

get relative() {
assert.ok(this.path)
return path.relative(this.base, this.path)
}

isBuffer() {
return Buffer.isBuffer(this.#contents)
}
}
20 changes: 10 additions & 10 deletions gogen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 9,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"engines": {
"node": ">= 10"
"node": ">= 10.3"
},
"files": [
"dist",
Expand All @@ -28,28 28,28 @@
"prepublishOnly": "yarn build",
"prebuild": "rm -rf dist",
"build": "yarn build:esbuild && yarn build:dts",
"build:webpack": "webpack",
"build:rollup": "rollup -c",
"inspect": "source-map-explorer $npm_package_main",
"build:esbuild": "esbuild $npm_package_source --outfile=$npm_package_main --bundle --minify --platform=node --target=node10.3 --format=cjs",
"build:esbuild-inspect": "yarn build:esbuild --sourcemap && yarn inspect",
"build:dts": "rollup -c rollup.config.dts.js",
"build:ncc": "ncc build lib/index.ts -o dist -m",
"build:esbuild": "esbuild $npm_package_source --outfile=$npm_package_main --bundle --minify --platform=node --target=node10 --format=cjs",
"build:inspect": "SOURCE_MAP=yes yarn build && source-map-explorer dist/index.js"
"build:rollup": "rollup -c",
"build:webpack": "webpack",
"build:webpack-inspect": "SOURCE_MAP=yes yarn build:webpack && yarn inspect",
"build:ncc": "ncc build lib/index.ts -o dist -m"
},
"devDependencies": {
"@types/lodash.template": "^4.5.0",
"@types/prompts": "^2.0.14",
"@types/through2": "^2.0.36",
"@types/vinyl-fs": "^2.4.12",
"esbuild": "^0.13.4",
"get-stream": "^6.0.1",
"fast-glob": "^3.2.7",
"kleur": "^3.0.0",
"lodash.template": "^4.4.0",
"mri": "^1.2.0",
"prompts": "^1.2.1",
"rollup": "^2.56.3",
"rollup-plugin-dts": "^4.0.0",
"through2": "^2.0.0",
"typescript": "^4.4.3",
"vinyl-fs": "^3.0.3"
"typescript": "^4.4.3"
}
}
2 changes: 1 addition & 1 deletion gogen/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
{
"include": ["lib"],
"compilerOptions": {
"target": "ES6",
"target": "ES2018",
"module": "ESNext",
"moduleResolution": "Node",
"strict": true,
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 5,8 @@
"examples/*"
],
"scripts": {
"postinstallDev": "yarn prepare",
"prepare": "yarn workspace gogen run build",
"prepare": "yarn build",
"build": "yarn workspace gogen run build",
"dev": "yarn workspace gogen run build:esbuild --watch",
"test": "jest",
"test:coverage": "jest --coverage",
Expand All @@ -29,6 29,7 @@
"readable-stream": "file:./gogen/alias/readable-stream"
},
"devDependencies": {
"@types/glob-parent": "^5.1.1",
"@types/jest": "^27.0.1",
"es-jest": "^1.3.0",
"jest": "^27.2.0",
Expand Down
Loading

0 comments on commit bb49881

Please sign in to comment.