From f6d4cba2d74e54f58ed1f5ab421fadcbeadf0d15 Mon Sep 17 00:00:00 2001 From: qiang Date: Tue, 23 Aug 2022 15:48:50 +0800 Subject: [PATCH] fix: static copy with path like glob --- .../__tests__/pathToGlob.spec.ts | 25 +++++++++++++++++++ packages/uni-cli-shared/src/utils.ts | 16 ++++++++++++ packages/uni-cli-shared/src/watcher.ts | 7 +++++- 3 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 packages/uni-cli-shared/__tests__/pathToGlob.spec.ts diff --git a/packages/uni-cli-shared/__tests__/pathToGlob.spec.ts b/packages/uni-cli-shared/__tests__/pathToGlob.spec.ts new file mode 100644 index 000000000..1aa865ac7 --- /dev/null +++ b/packages/uni-cli-shared/__tests__/pathToGlob.spec.ts @@ -0,0 +1,25 @@ +import { pathToGlob } from '../src/utils' + +describe('pathToGlob', () => { + it('path in unix', () => { + expect(pathToGlob('/test', '**/*.js')).toBe('/test/**/*.js') + expect(pathToGlob('/test(1', '**/*.js')).toBe('/test[(]1/**/*.js') + expect(pathToGlob('/test(1', '**/*.js', { escape: true })).toBe( + '/test\\(1/**/*.js' + ) + }) + it('path in windows', () => { + expect(pathToGlob('C:\\\\test\\test', '**/*.js', { windows: true })).toBe( + 'C:/test/test/**/*.js' + ) + expect(pathToGlob('C:\\\\test\\test(1', '**/*.js', { windows: true })).toBe( + 'C:/test/test[(]1/**/*.js' + ) + expect( + pathToGlob('C:\\\\test\\test(1', '**/*.js', { + windows: true, + escape: true, + }) + ).toBe('C:/test/test[(]1/**/*.js') + }) +}) diff --git a/packages/uni-cli-shared/src/utils.ts b/packages/uni-cli-shared/src/utils.ts index 1a6e564a0..aa35534f9 100644 --- a/packages/uni-cli-shared/src/utils.ts +++ b/packages/uni-cli-shared/src/utils.ts @@ -91,3 +91,19 @@ export function normalizeParsePlugins( if (isTS) plugins.push('typescript', 'decorators-legacy') return plugins } + +export function pathToGlob( + pathString: string, + glob: string, + options: { windows?: boolean; escape?: boolean } = {} +): string { + const isWindows = + 'windows' in options ? options.windows : /^win/.test(process.platform) + const useEscape = options.escape + const str = isWindows ? pathString.replace(/\\/g, '/') : pathString + let safeStr = str.replace( + /[\\*?[\]{}()!]/g, + isWindows || !useEscape ? '[$&]' : '\\$&' + ) + return path.posix.join(safeStr, glob) +} diff --git a/packages/uni-cli-shared/src/watcher.ts b/packages/uni-cli-shared/src/watcher.ts index bb7c9eca8..ad70ebef6 100644 --- a/packages/uni-cli-shared/src/watcher.ts +++ b/packages/uni-cli-shared/src/watcher.ts @@ -2,6 +2,7 @@ import fs from 'fs-extra' import path from 'path' import { FSWatcher, watch, WatchOptions } from 'chokidar' import { isArray } from '@vue/shared' +import { pathToGlob } from './utils' type FileTransform = (source: Buffer, filename: string) => void | string export interface FileWatcherOptions { src: string | string[] @@ -30,7 +31,11 @@ export class FileWatcher { if (!this.watcher) { const copy = this.copy.bind(this) const remove = this.remove.bind(this) - this.watcher = watch(this.src, watchOptions) + // escape chokidar cwd + const src = this.src.map((src) => + pathToGlob(path.resolve(watchOptions.cwd), src) + ) + this.watcher = watch(src, watchOptions) .on('add', copy) .on('addDir', copy) .on('change', copy) -- GitLab