@mlaursen/copy-scss-files 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +3 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +89 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +54 -0
- package/src/copyToDist.ts +11 -0
- package/src/ensureParentDir.ts +10 -0
- package/src/index.ts +64 -0
- package/src/logger.ts +18 -0
- package/src/watcher.ts +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Mikkel Laursen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
interface CopyScssFilesOptions {
|
|
2
|
+
/** @defaultValue `"src"` */
|
|
3
|
+
src?: string;
|
|
4
|
+
/** @defaultValue `"dist"` */
|
|
5
|
+
dist?: string;
|
|
6
|
+
/** @defaultValue `"${src}/**\/*.scss"` */
|
|
7
|
+
pattern?: string;
|
|
8
|
+
/** @defaultValue `process.argv.includes("--watch")` */
|
|
9
|
+
watch?: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* @defaultValue `(path, renameDist) => [renameDist(path)]`
|
|
12
|
+
*/
|
|
13
|
+
getDistPaths?: (path: string, renameDist: (path: string) => string) => readonly string[];
|
|
14
|
+
}
|
|
15
|
+
declare function copyScssFiles(options: CopyScssFilesOptions): Promise<void>;
|
|
16
|
+
|
|
17
|
+
export { copyScssFiles };
|
|
18
|
+
export type { CopyScssFilesOptions };
|
|
19
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sources":["../types/index.d.ts"],"sourcesContent":["export interface CopyScssFilesOptions {\n /** @defaultValue `\"src\"` */\n src?: string;\n /** @defaultValue `\"dist\"` */\n dist?: string;\n /** @defaultValue `\"${src}/**\\/*.scss\"` */\n pattern?: string;\n /** @defaultValue `process.argv.includes(\"--watch\")` */\n watch?: boolean;\n /**\n * @defaultValue `(path, renameDist) => [renameDist(path)]`\n */\n getDistPaths?: (path: string, renameDist: (path: string) => string) => readonly string[];\n}\nexport declare function copyScssFiles(options: CopyScssFilesOptions): Promise<void>;\n//# sourceMappingURL=index.d.ts.map"],"names":[],"mappings":"AAAO,UAAA,oBAAA;AACP;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,iBAAA,aAAA,UAAA,oBAAA,GAAA,OAAA;;;;"}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { glob } from 'glob';
|
|
2
|
+
import { mkdir, copyFile, rm } from 'node:fs/promises';
|
|
3
|
+
import { existsSync } from 'node:fs';
|
|
4
|
+
import { dirname } from 'node:path';
|
|
5
|
+
import chokidar from 'chokidar';
|
|
6
|
+
|
|
7
|
+
const ensureParentDir = async (filePath)=>{
|
|
8
|
+
const folder = dirname(filePath);
|
|
9
|
+
if (!existsSync(folder)) {
|
|
10
|
+
await mkdir(folder, {
|
|
11
|
+
recursive: true
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
let _log = false;
|
|
17
|
+
function enableLogger() {
|
|
18
|
+
_log = true;
|
|
19
|
+
}
|
|
20
|
+
function log(msg) {
|
|
21
|
+
if (!_log) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
// eslint-disable-next-line no-console
|
|
25
|
+
console.log(msg);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
async function copyToDist(path, dest) {
|
|
29
|
+
await ensureParentDir(dest);
|
|
30
|
+
await copyFile(path, dest);
|
|
31
|
+
log(`Copied ${path} -> ${dest}`);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function createWatcher({ watchPath, getDistPaths }) {
|
|
35
|
+
const watcher = chokidar.watch(watchPath, {
|
|
36
|
+
ignored: (path, stats)=>!!stats?.isFile() && !path.endsWith(".scss")
|
|
37
|
+
});
|
|
38
|
+
watcher.on("all", async (eventName, path)=>{
|
|
39
|
+
const paths = getDistPaths(path);
|
|
40
|
+
switch(eventName){
|
|
41
|
+
case "add":
|
|
42
|
+
case "change":
|
|
43
|
+
void Promise.all(paths.map((distPath)=>copyToDist(path, distPath)));
|
|
44
|
+
break;
|
|
45
|
+
case "unlink":
|
|
46
|
+
{
|
|
47
|
+
void Promise.all(paths.map(async (distPath)=>{
|
|
48
|
+
if (existsSync(distPath)) {
|
|
49
|
+
await rm(distPath);
|
|
50
|
+
log(`Removed ${distPath}`);
|
|
51
|
+
}
|
|
52
|
+
}));
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
watcher.on("ready", ()=>{
|
|
58
|
+
enableLogger();
|
|
59
|
+
log("Watching changes...");
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function copyScssFiles(options) {
|
|
64
|
+
const { src = "src", dist = "dist", watch = process.argv.includes("--watch"), pattern = `${src}/**/*.scss`, getDistPaths = (path, renameDist)=>[
|
|
65
|
+
renameDist(path)
|
|
66
|
+
] } = options;
|
|
67
|
+
const renameDist = (path)=>path.replace(src, dist);
|
|
68
|
+
const resolveDistPaths = (path)=>getDistPaths(path, renameDist);
|
|
69
|
+
if (watch) {
|
|
70
|
+
createWatcher({
|
|
71
|
+
watchPath: src,
|
|
72
|
+
getDistPaths: resolveDistPaths
|
|
73
|
+
});
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
await ensureParentDir(dist);
|
|
77
|
+
const styles = await glob(pattern);
|
|
78
|
+
for (const filePath of styles){
|
|
79
|
+
const destFiles = resolveDistPaths(filePath);
|
|
80
|
+
for (const destPath of destFiles){
|
|
81
|
+
await copyToDist(filePath, destPath);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
enableLogger();
|
|
85
|
+
log(`Copied ${styles.length} scss files`);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export { copyScssFiles };
|
|
89
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../src/ensureParentDir.ts","../src/logger.ts","../src/copyToDist.ts","../src/watcher.ts","../src/index.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { mkdir } from \"node:fs/promises\";\nimport { dirname } from \"node:path\";\n\nexport const ensureParentDir = async (filePath: string): Promise<void> => {\n const folder = dirname(filePath);\n if (!existsSync(folder)) {\n await mkdir(folder, { recursive: true });\n }\n};\n","let _log = false;\n\nexport function enableLogger(): void {\n _log = true;\n}\n\nexport function disableLogger(): void {\n _log = false;\n}\n\nexport function log(msg: string): void {\n if (!_log) {\n return;\n }\n\n // eslint-disable-next-line no-console\n console.log(msg);\n}\n","import { copyFile } from \"node:fs/promises\";\n\nimport { ensureParentDir } from \"./ensureParentDir.js\";\nimport { log } from \"./logger.js\";\n\nexport async function copyToDist(path: string, dest: string): Promise<void> {\n await ensureParentDir(dest);\n await copyFile(path, dest);\n\n log(`Copied ${path} -> ${dest}`);\n}\n","import chokidar from \"chokidar\";\nimport { existsSync } from \"node:fs\";\nimport { rm } from \"node:fs/promises\";\n\nimport { copyToDist } from \"./copyToDist.js\";\nimport { enableLogger, log } from \"./logger.js\";\n\ninterface WatcherOptions {\n watchPath: string;\n getDistPaths: (path: string) => readonly string[];\n}\n\nexport function createWatcher({\n watchPath,\n getDistPaths,\n}: WatcherOptions): void {\n const watcher = chokidar.watch(watchPath, {\n ignored: (path, stats) => !!stats?.isFile() && !path.endsWith(\".scss\"),\n });\n\n watcher.on(\"all\", async (eventName, path) => {\n const paths = getDistPaths(path);\n\n switch (eventName) {\n case \"add\":\n case \"change\":\n void Promise.all(paths.map((distPath) => copyToDist(path, distPath)));\n break;\n case \"unlink\": {\n void Promise.all(\n paths.map(async (distPath) => {\n if (existsSync(distPath)) {\n await rm(distPath);\n log(`Removed ${distPath}`);\n }\n })\n );\n break;\n }\n }\n });\n\n watcher.on(\"ready\", () => {\n enableLogger();\n log(\"Watching changes...\");\n });\n}\n","import { glob } from \"glob\";\n\nimport { copyToDist } from \"./copyToDist.js\";\nimport { ensureParentDir } from \"./ensureParentDir.js\";\nimport { enableLogger, log } from \"./logger.js\";\nimport { createWatcher } from \"./watcher.js\";\n\nexport interface CopyScssFilesOptions {\n /** @defaultValue `\"src\"` */\n src?: string;\n\n /** @defaultValue `\"dist\"` */\n dist?: string;\n\n /** @defaultValue `\"${src}/**\\/*.scss\"` */\n pattern?: string;\n\n /** @defaultValue `process.argv.includes(\"--watch\")` */\n watch?: boolean;\n\n /**\n * @defaultValue `(path, renameDist) => [renameDist(path)]`\n */\n getDistPaths?: (\n path: string,\n renameDist: (path: string) => string\n ) => readonly string[];\n}\n\nexport async function copyScssFiles(\n options: CopyScssFilesOptions\n): Promise<void> {\n const {\n src = \"src\",\n dist = \"dist\",\n watch = process.argv.includes(\"--watch\"),\n pattern = `${src}/**/*.scss`,\n getDistPaths = (path, renameDist) => [renameDist(path)],\n } = options;\n\n const renameDist = (path: string): string => path.replace(src, dist);\n const resolveDistPaths = (path: string): readonly string[] =>\n getDistPaths(path, renameDist);\n\n if (watch) {\n createWatcher({\n watchPath: src,\n getDistPaths: resolveDistPaths,\n });\n return;\n }\n\n await ensureParentDir(dist);\n const styles = await glob(pattern);\n for (const filePath of styles) {\n const destFiles = resolveDistPaths(filePath);\n for (const destPath of destFiles) {\n await copyToDist(filePath, destPath);\n }\n }\n\n enableLogger();\n log(`Copied ${styles.length} scss files`);\n}\n"],"names":["ensureParentDir","filePath","folder","dirname","existsSync","mkdir","recursive","_log","enableLogger","log","msg","console","copyToDist","path","dest","copyFile","createWatcher","watchPath","getDistPaths","watcher","chokidar","watch","ignored","stats","isFile","endsWith","on","eventName","paths","Promise","all","map","distPath","rm","copyScssFiles","options","src","dist","process","argv","includes","pattern","renameDist","replace","resolveDistPaths","styles","glob","destFiles","destPath","length"],"mappings":";;;;;;AAIO,MAAMA,kBAAkB,OAAOC,QAAAA,GAAAA;AACpC,IAAA,MAAMC,SAASC,OAAAA,CAAQF,QAAAA,CAAAA;IACvB,IAAI,CAACG,WAAWF,MAAAA,CAAAA,EAAS;AACvB,QAAA,MAAMG,MAAMH,MAAAA,EAAQ;YAAEI,SAAAA,EAAW;AAAK,SAAA,CAAA;AACxC,IAAA;AACF,CAAA;;ACTA,IAAIC,IAAAA,GAAO,KAAA;AAEJ,SAASC,YAAAA,GAAAA;IACdD,IAAAA,GAAO,IAAA;AACT;AA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AAA,MAAME,YAAYH,gBAAAA,CAAiB3C,QAAAA,CAAAA;QACnC,KAAK,MAAM+C,YAAYD,SAAAA,CAAW;AAChC,YAAA,MAAMnC,WAAWX,QAAAA,EAAU+C,QAAAA,CAAAA;AAC7B,QAAA;AACF,IAAA;AAEAxC,IAAAA,YAAAA,EAAAA;AACAC,IAAAA,GAAAA,CAAI,CAAC,OAAO,EAAEoC,OAAOI,MAAM,CAAC,WAAW,CAAC,CAAA;AAC1C;;;;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mlaursen/copy-scss-files",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"description": "A small util to copy SCSS files for publishing.",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"default": "./dist/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"./package.json": "./package.json"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"chokidar": "^5.0.0",
|
|
15
|
+
"glob": "^13.0.0"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@rollup/plugin-node-resolve": "^16.0.3",
|
|
19
|
+
"@trivago/prettier-plugin-sort-imports": "^6.0.0",
|
|
20
|
+
"@types/node": "^24.10.1",
|
|
21
|
+
"concurrently": "^9.2.1",
|
|
22
|
+
"eslint": "^9.39.1",
|
|
23
|
+
"prettier": "^3.7.4",
|
|
24
|
+
"rollup": "^4.53.3",
|
|
25
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
26
|
+
"rollup-plugin-swc3": "^0.12.1",
|
|
27
|
+
"typescript": "^5.9.3",
|
|
28
|
+
"@mlaursen/eslint-config": "10.1.0"
|
|
29
|
+
},
|
|
30
|
+
"publishConfig": {
|
|
31
|
+
"access": "public"
|
|
32
|
+
},
|
|
33
|
+
"volta": {
|
|
34
|
+
"node": "24.11.0",
|
|
35
|
+
"pnpm": "10.20.0"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"clean-dist": "rm -rf dist types",
|
|
39
|
+
"clean-cache": "rm -rf .turbo node_modules",
|
|
40
|
+
"clean": "concurrently 'pnpm clean-dist' 'pnpm clean-cache'",
|
|
41
|
+
"check-format": "prettier --check .",
|
|
42
|
+
"format": "prettier --write .",
|
|
43
|
+
"lint": "eslint .",
|
|
44
|
+
"lint-fix": "pnpm lint --fix",
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"typecheck-watch": "pnpm typecheck --watch",
|
|
47
|
+
"build-dist": "rollup -c rollup.config.ts",
|
|
48
|
+
"build-dist-watch": "pnpm build-dist --watch",
|
|
49
|
+
"build-types": "tsc -p tsconfig.types.json",
|
|
50
|
+
"build-types-watch": "pnpm build-types --watch",
|
|
51
|
+
"build": "pnpm build-types && pnpm build-dist",
|
|
52
|
+
"dev": "concurrently 'pnpm build-types-watch' 'pnpm build-dist-watch'"
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { copyFile } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
import { ensureParentDir } from "./ensureParentDir.js";
|
|
4
|
+
import { log } from "./logger.js";
|
|
5
|
+
|
|
6
|
+
export async function copyToDist(path: string, dest: string): Promise<void> {
|
|
7
|
+
await ensureParentDir(dest);
|
|
8
|
+
await copyFile(path, dest);
|
|
9
|
+
|
|
10
|
+
log(`Copied ${path} -> ${dest}`);
|
|
11
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { mkdir } from "node:fs/promises";
|
|
3
|
+
import { dirname } from "node:path";
|
|
4
|
+
|
|
5
|
+
export const ensureParentDir = async (filePath: string): Promise<void> => {
|
|
6
|
+
const folder = dirname(filePath);
|
|
7
|
+
if (!existsSync(folder)) {
|
|
8
|
+
await mkdir(folder, { recursive: true });
|
|
9
|
+
}
|
|
10
|
+
};
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { glob } from "glob";
|
|
2
|
+
|
|
3
|
+
import { copyToDist } from "./copyToDist.js";
|
|
4
|
+
import { ensureParentDir } from "./ensureParentDir.js";
|
|
5
|
+
import { enableLogger, log } from "./logger.js";
|
|
6
|
+
import { createWatcher } from "./watcher.js";
|
|
7
|
+
|
|
8
|
+
export interface CopyScssFilesOptions {
|
|
9
|
+
/** @defaultValue `"src"` */
|
|
10
|
+
src?: string;
|
|
11
|
+
|
|
12
|
+
/** @defaultValue `"dist"` */
|
|
13
|
+
dist?: string;
|
|
14
|
+
|
|
15
|
+
/** @defaultValue `"${src}/**\/*.scss"` */
|
|
16
|
+
pattern?: string;
|
|
17
|
+
|
|
18
|
+
/** @defaultValue `process.argv.includes("--watch")` */
|
|
19
|
+
watch?: boolean;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @defaultValue `(path, renameDist) => [renameDist(path)]`
|
|
23
|
+
*/
|
|
24
|
+
getDistPaths?: (
|
|
25
|
+
path: string,
|
|
26
|
+
renameDist: (path: string) => string
|
|
27
|
+
) => readonly string[];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function copyScssFiles(
|
|
31
|
+
options: CopyScssFilesOptions
|
|
32
|
+
): Promise<void> {
|
|
33
|
+
const {
|
|
34
|
+
src = "src",
|
|
35
|
+
dist = "dist",
|
|
36
|
+
watch = process.argv.includes("--watch"),
|
|
37
|
+
pattern = `${src}/**/*.scss`,
|
|
38
|
+
getDistPaths = (path, renameDist) => [renameDist(path)],
|
|
39
|
+
} = options;
|
|
40
|
+
|
|
41
|
+
const renameDist = (path: string): string => path.replace(src, dist);
|
|
42
|
+
const resolveDistPaths = (path: string): readonly string[] =>
|
|
43
|
+
getDistPaths(path, renameDist);
|
|
44
|
+
|
|
45
|
+
if (watch) {
|
|
46
|
+
createWatcher({
|
|
47
|
+
watchPath: src,
|
|
48
|
+
getDistPaths: resolveDistPaths,
|
|
49
|
+
});
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
await ensureParentDir(dist);
|
|
54
|
+
const styles = await glob(pattern);
|
|
55
|
+
for (const filePath of styles) {
|
|
56
|
+
const destFiles = resolveDistPaths(filePath);
|
|
57
|
+
for (const destPath of destFiles) {
|
|
58
|
+
await copyToDist(filePath, destPath);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
enableLogger();
|
|
63
|
+
log(`Copied ${styles.length} scss files`);
|
|
64
|
+
}
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
let _log = false;
|
|
2
|
+
|
|
3
|
+
export function enableLogger(): void {
|
|
4
|
+
_log = true;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function disableLogger(): void {
|
|
8
|
+
_log = false;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function log(msg: string): void {
|
|
12
|
+
if (!_log) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line no-console
|
|
17
|
+
console.log(msg);
|
|
18
|
+
}
|
package/src/watcher.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import chokidar from "chokidar";
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import { rm } from "node:fs/promises";
|
|
4
|
+
|
|
5
|
+
import { copyToDist } from "./copyToDist.js";
|
|
6
|
+
import { enableLogger, log } from "./logger.js";
|
|
7
|
+
|
|
8
|
+
interface WatcherOptions {
|
|
9
|
+
watchPath: string;
|
|
10
|
+
getDistPaths: (path: string) => readonly string[];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function createWatcher({
|
|
14
|
+
watchPath,
|
|
15
|
+
getDistPaths,
|
|
16
|
+
}: WatcherOptions): void {
|
|
17
|
+
const watcher = chokidar.watch(watchPath, {
|
|
18
|
+
ignored: (path, stats) => !!stats?.isFile() && !path.endsWith(".scss"),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
watcher.on("all", async (eventName, path) => {
|
|
22
|
+
const paths = getDistPaths(path);
|
|
23
|
+
|
|
24
|
+
switch (eventName) {
|
|
25
|
+
case "add":
|
|
26
|
+
case "change":
|
|
27
|
+
void Promise.all(paths.map((distPath) => copyToDist(path, distPath)));
|
|
28
|
+
break;
|
|
29
|
+
case "unlink": {
|
|
30
|
+
void Promise.all(
|
|
31
|
+
paths.map(async (distPath) => {
|
|
32
|
+
if (existsSync(distPath)) {
|
|
33
|
+
await rm(distPath);
|
|
34
|
+
log(`Removed ${distPath}`);
|
|
35
|
+
}
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
watcher.on("ready", () => {
|
|
44
|
+
enableLogger();
|
|
45
|
+
log("Watching changes...");
|
|
46
|
+
});
|
|
47
|
+
}
|