-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove dependency on copy-template-dir (#6659)
- Loading branch information
Showing
15 changed files
with
190 additions
and
2,167 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
// License for copy-template-dir. | ||
// Original repository: https://github.com/yoshuawuyts/copy-template-dir | ||
|
||
// The MIT License (MIT) | ||
|
||
// Copyright (c) 2015 Yoshua Wuyts | ||
|
||
// 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. | ||
|
||
import assert from 'assert' | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { pipeline } from 'stream' | ||
import { promisify } from 'util' | ||
|
||
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'maxstache... Remove this comment to see the full error message | ||
import maxstache from 'maxstache' | ||
// @ts-expect-error TS(7016) FIXME: Could not find a declaration file for module 'maxstache-stream... Remove this comment to see the full error message | ||
import maxstacheStream from 'maxstache-stream' | ||
import readdirp, { EntryInfo, ReaddirpStream } from 'readdirp' | ||
|
||
const noop = (): void => undefined | ||
|
||
// Remove a leading underscore | ||
function removeUnderscore(filepath: string): string { | ||
const parts = filepath.split(path.sep) | ||
const filename = parts.pop()?.replace(/^_/, '') || '' | ||
return [...parts, filename].join(path.sep) | ||
} | ||
|
||
// Write a file to a directory | ||
async function writeFile(outDir: string, vars: Record<string, string>, file: EntryInfo): Promise<void> { | ||
const fileName = file.path | ||
const inFile = file.fullPath | ||
const parentDir = path.dirname(file.path) | ||
const outFile = path.join(outDir, maxstache(removeUnderscore(fileName), vars)) | ||
|
||
await fs.promises.mkdir(path.join(outDir, maxstache(parentDir, vars)), { recursive: true }) | ||
|
||
const rs = fs.createReadStream(inFile) | ||
const ts = maxstacheStream(vars) | ||
const ws = fs.createWriteStream(outFile) | ||
|
||
await promisify(pipeline)(rs, ts, ws) | ||
} | ||
|
||
// High throughput template dir writes | ||
export async function copyTemplateDir(srcDir: string, outDir: string, vars: any): Promise<string[]> { | ||
if (!vars) vars = noop | ||
|
||
assert.strictEqual(typeof srcDir, 'string') | ||
assert.strictEqual(typeof outDir, 'string') | ||
assert.strictEqual(typeof vars, 'object') | ||
|
||
await fs.promises.mkdir(outDir, { recursive: true }) | ||
|
||
const rs: ReaddirpStream = readdirp(srcDir) | ||
const streams: Promise<void>[] = [] | ||
const createdFiles: string[] = [] | ||
|
||
rs.on('data', (file: EntryInfo) => { | ||
createdFiles.push(path.join(outDir, maxstache(removeUnderscore(file.path), vars))) | ||
streams.push(writeFile(outDir, vars, file)) | ||
}) | ||
|
||
await new Promise<void>((resolve, reject) => { | ||
rs.on('end', async () => { | ||
try { | ||
await Promise.all(streams) | ||
resolve() | ||
} catch (error) { | ||
reject(error) | ||
} | ||
}) | ||
rs.on('error', (error) => { | ||
reject(error) | ||
}) | ||
}) | ||
|
||
return createdFiles | ||
} |
90 changes: 90 additions & 0 deletions
90
tests/unit/utils/copy-template-dir/copy-template-dir.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import fs from 'fs' | ||
import path from 'path' | ||
import { fileURLToPath } from 'url' | ||
|
||
import readdirp from 'readdirp' | ||
import { describe, expect, test } from 'vitest' | ||
|
||
import { copyTemplateDir } from '../../../../dist/utils/copy-template-dir/copy-template-dir.js' | ||
|
||
// eslint-disable-next-line no-underscore-dangle | ||
const __filename = fileURLToPath(import.meta.url) | ||
// eslint-disable-next-line no-underscore-dangle | ||
const __dirname = path.dirname(__filename) | ||
|
||
describe('copyTemplateDir', () => { | ||
test('should assert input values', async () => { | ||
await expect(copyTemplateDir()).rejects.toThrow(/string/) | ||
await expect(copyTemplateDir('foo')).rejects.toThrow(/string/) | ||
await expect(copyTemplateDir('foo', 'bar', 'err')).rejects.toThrow(/object/) | ||
}) | ||
|
||
test('should write a bunch of files', async () => { | ||
const checkCreatedFileNames = (names) => { | ||
expect(names).toContain('.a') | ||
expect(names).toContain('c') | ||
expect(names).toContain('1.txt') | ||
expect(names).toContain('2.txt') | ||
expect(names).toContain('3.txt') | ||
expect(names).toContain('.txt') | ||
expect(names).toContain(`foo${path.sep}.b`) | ||
expect(names).toContain(`foo${path.sep}d`) | ||
expect(names).toContain(`foo${path.sep}4.txt`) | ||
} | ||
|
||
const inDir = path.join(__dirname, 'fixtures') | ||
const outDir = path.join(__dirname, '../tmp') | ||
|
||
const createdFiles = await copyTemplateDir(inDir, outDir, {}) | ||
|
||
expect(Array.isArray(createdFiles)).toBe(true) | ||
expect(createdFiles.length).toBe(10) | ||
|
||
// Checks the direct output of the function, to ensure names are correct | ||
checkCreatedFileNames(createdFiles.map((filePath) => path.relative(outDir, filePath))) | ||
|
||
// Checks that the files were created in the file system | ||
const files = await readdirp.promise(outDir) | ||
checkCreatedFileNames(files.map((file) => file.path)) | ||
|
||
// Cleanup | ||
fs.rmdirSync(outDir, { recursive: true }) | ||
}) | ||
|
||
test('should inject context variables strings', async () => { | ||
const inDir = path.join(__dirname, 'fixtures') | ||
const outDir = path.join(__dirname, '../tmp') | ||
|
||
await copyTemplateDir(inDir, outDir, { foo: 'bar' }) | ||
|
||
const fileContent = fs.readFileSync(path.join(outDir, '1.txt'), 'utf-8').trim() | ||
expect(fileContent).toBe('hello bar sama') | ||
|
||
// Cleanup | ||
fs.rmdirSync(outDir, { recursive: true }) | ||
}) | ||
|
||
test('should inject context variables strings into filenames', async () => { | ||
const inDir = path.join(__dirname, 'fixtures') | ||
const outDir = path.join(__dirname, '../tmp') | ||
|
||
await copyTemplateDir(inDir, outDir, { foo: 'bar' }) | ||
|
||
expect(fs.existsSync(path.join(outDir, 'bar.txt'))).toBe(true) | ||
|
||
// Cleanup | ||
fs.rmdirSync(outDir, { recursive: true }) | ||
}) | ||
|
||
test('should inject context variables strings into directory names', async () => { | ||
const inDir = path.join(__dirname, 'fixtures') | ||
const outDir = path.join(__dirname, '../tmp') | ||
|
||
await copyTemplateDir(inDir, outDir, { foo: 'bar' }) | ||
|
||
expect(fs.existsSync(path.join(outDir, 'bar'))).toBe(true) | ||
|
||
// Cleanup | ||
fs.rmdirSync(outDir, { recursive: true }) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
hello {{foo}} sama |
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
a158331
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📊 Benchmark results
a158331
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📊 Benchmark results