@hkamran/utility-assets 1.0.0
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/.eslintignore +8 -0
- package/.eslintrc.js +9 -0
- package/.prettierignore +10 -0
- package/README.md +12 -0
- package/index.d.ts +17 -0
- package/index.js +176 -0
- package/package.json +52 -0
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
package/.prettierignore
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# `@hkamran/utility-assets`
|
|
2
|
+
|
|
3
|
+
[](../../LICENSE.md)
|
|
4
|
+
[](https://badge.fury.io/js/%40hkamran%2Futility-assets.svg)
|
|
5
|
+
|
|
6
|
+
A quick way to set up an assets site
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm i @hkamran/utility-assets
|
|
12
|
+
```
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function minifyJSON(
|
|
2
|
+
basePath: string,
|
|
3
|
+
outputDir: string,
|
|
4
|
+
excludeSchemas?: string,
|
|
5
|
+
): Promise<void>;
|
|
6
|
+
export function minifyCSS(basePath: string, outputDir: string): Promise<void>;
|
|
7
|
+
export function moveFiles(
|
|
8
|
+
basePath: string,
|
|
9
|
+
outputDir: string,
|
|
10
|
+
ignoredFilenameContents?: string[],
|
|
11
|
+
ignoredFileExtensions?: string[],
|
|
12
|
+
): Promise<void>;
|
|
13
|
+
export function moveFolder(basePath: string, outputDir: string): Promise<void>;
|
|
14
|
+
/** @type {string} */
|
|
15
|
+
export const basePath: string;
|
|
16
|
+
/** @type {string} */
|
|
17
|
+
export const baseOutputPath: string;
|
package/index.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import {
|
|
2
|
+
constants,
|
|
3
|
+
copyFile,
|
|
4
|
+
mkdir,
|
|
5
|
+
readdir,
|
|
6
|
+
readFile,
|
|
7
|
+
writeFile,
|
|
8
|
+
} from "fs/promises";
|
|
9
|
+
import { join } from "path";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Minify all JSON files
|
|
13
|
+
* @param {string} basePath The base path to use
|
|
14
|
+
* @param {string} [excludeSchemas=true] Exclude schemas from the final product (optional, defaults to `true`)
|
|
15
|
+
* @param {string} outputDir The output directory
|
|
16
|
+
*/
|
|
17
|
+
export const minifyJSON = async (
|
|
18
|
+
basePath,
|
|
19
|
+
outputDir,
|
|
20
|
+
excludeSchemas = true,
|
|
21
|
+
) => {
|
|
22
|
+
const files = (await readdir(basePath, { withFileTypes: true }))
|
|
23
|
+
.filter((item) => item.isFile())
|
|
24
|
+
.map((item) => item.name)
|
|
25
|
+
.filter(
|
|
26
|
+
(filename) =>
|
|
27
|
+
!filename.startsWith(".") &&
|
|
28
|
+
!filename.startsWith("_") &&
|
|
29
|
+
(excludeSchemas ? !filename.includes("schema") : true) &&
|
|
30
|
+
filename.endsWith("json") &&
|
|
31
|
+
filename !== "package.json",
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
files.forEach(
|
|
35
|
+
async (filename) =>
|
|
36
|
+
await writeFile(
|
|
37
|
+
join(outputDir, filename),
|
|
38
|
+
JSON.stringify(
|
|
39
|
+
JSON.parse(await readFile(join(basePath, filename))),
|
|
40
|
+
),
|
|
41
|
+
),
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Minify all CSS files
|
|
47
|
+
*
|
|
48
|
+
* Minification code courtesy of [@derder56 on Dev.to](https://dev.to/derder56/how-to-build-a-css-minifier-with-8-lines-of-javascript-4bj3)
|
|
49
|
+
*
|
|
50
|
+
* @param {string} basePath The base path to use
|
|
51
|
+
* @param {string} outputDir The output directory
|
|
52
|
+
*/
|
|
53
|
+
export const minifyCSS = async (basePath, outputDir) => {
|
|
54
|
+
const files = (await readdir(basePath, { withFileTypes: true }))
|
|
55
|
+
.filter((item) => item.isFile())
|
|
56
|
+
.map((item) => item.name)
|
|
57
|
+
.filter(
|
|
58
|
+
(filename) =>
|
|
59
|
+
!filename.startsWith(".") &&
|
|
60
|
+
!filename.startsWith("_") &&
|
|
61
|
+
filename.endsWith("css"),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
files.forEach(
|
|
65
|
+
async (filename) =>
|
|
66
|
+
await writeFile(
|
|
67
|
+
join(outputDir, filename),
|
|
68
|
+
await (
|
|
69
|
+
await readFile(join(basePath, filename))
|
|
70
|
+
)
|
|
71
|
+
.toString()
|
|
72
|
+
.replace(/([^0-9a-zA-Z\.#])\s+/g, "$1")
|
|
73
|
+
.replace(/\s([^0-9a-zA-Z\.#]+)/g, "$1")
|
|
74
|
+
.replace(/;}/g, "}")
|
|
75
|
+
.replace(/\/\*.*?\*\//g, ""),
|
|
76
|
+
),
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Check if a string contains any ignored contents
|
|
82
|
+
* @param {string} check The string to check
|
|
83
|
+
* @param {string[]} ignoredContents The contents to check against
|
|
84
|
+
* @returns {boolean} Whether the string contains any of the `ignoredContents`
|
|
85
|
+
*/
|
|
86
|
+
const checkIgnoredContents = (check, ignoredContents) => {
|
|
87
|
+
for (const ignoredContent of ignoredContents) {
|
|
88
|
+
if (check.includes(ignoredContent)) {
|
|
89
|
+
return true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return false;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Check if a string ends with any ignored endings
|
|
98
|
+
* @param {string} check The string to check
|
|
99
|
+
* @param {string[]} ignoredEndings The endings to check against
|
|
100
|
+
* @returns {boolean} Whether the string ends with any of the `ignoredEndings`
|
|
101
|
+
*/
|
|
102
|
+
const checkIgnoredEndings = (check, ignoredEndings) => {
|
|
103
|
+
for (const ignoredEnding of ignoredEndings) {
|
|
104
|
+
if (check.endsWith(ignoredEnding)) {
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return false;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Move files
|
|
114
|
+
* @param {string} basePath The base path to use
|
|
115
|
+
* @param {string[]} [ignoredFilenameContents=[schema]] Skip filenames if they include certain snippets (optional, defaults to `["schema"]`)
|
|
116
|
+
* @param {string[]} [ignoredFileExtensions=[json,css,toml,md]] Skip filenames if they have certain extensions (optional, defaults to `["json", "css", "toml", "md"]`)
|
|
117
|
+
* @param {string} outputDir The output directory (optional)
|
|
118
|
+
*/
|
|
119
|
+
export const moveFiles = async (
|
|
120
|
+
basePath,
|
|
121
|
+
outputDir,
|
|
122
|
+
ignoredFilenameContents = ["schema"],
|
|
123
|
+
ignoredFileExtensions = ["json", "css", "toml", "md"],
|
|
124
|
+
) => {
|
|
125
|
+
(await readdir(basePath, { withFileTypes: true }))
|
|
126
|
+
.filter((item) => item.isFile())
|
|
127
|
+
.map((item) => item.name)
|
|
128
|
+
.filter(
|
|
129
|
+
(filename) =>
|
|
130
|
+
!filename.startsWith(".") &&
|
|
131
|
+
!checkIgnoredContents(filename, ignoredFilenameContents) &&
|
|
132
|
+
!checkIgnoredEndings(filename, ignoredFileExtensions),
|
|
133
|
+
)
|
|
134
|
+
.forEach(
|
|
135
|
+
async (filename) =>
|
|
136
|
+
await copyFile(
|
|
137
|
+
join(basePath, filename),
|
|
138
|
+
join(outputDir, filename),
|
|
139
|
+
constants.COPYFILE_EXCL,
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Move a folder and its file
|
|
146
|
+
* @param {string} basePath The base path to use
|
|
147
|
+
* @param {string} outputDir The output directory (optional)
|
|
148
|
+
*/
|
|
149
|
+
export const moveFolder = async (basePath, outputDir) => {
|
|
150
|
+
if (outputDir) {
|
|
151
|
+
await mkdir(outputDir);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
(await readdir(basePath, { withFileTypes: true })).forEach(async (item) => {
|
|
155
|
+
if (item.isFile()) {
|
|
156
|
+
await copyFile(
|
|
157
|
+
join(basePath, item.name),
|
|
158
|
+
join(outputDir, item.name),
|
|
159
|
+
constants.COPYFILE_EXCL,
|
|
160
|
+
);
|
|
161
|
+
} else if (item.isDirectory()) {
|
|
162
|
+
await moveFolder(
|
|
163
|
+
join(basePath, item.name),
|
|
164
|
+
join(outputDir, item.name),
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/** @type {string} */
|
|
171
|
+
export const basePath = process.cwd();
|
|
172
|
+
|
|
173
|
+
/** @type {string} */
|
|
174
|
+
export const baseOutputPath = join(basePath, "dist");
|
|
175
|
+
|
|
176
|
+
await mkdir(baseOutputPath);
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hkamran/utility-assets",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A quick way to set up an assets site",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"author": {
|
|
9
|
+
"name": "H. Kamran",
|
|
10
|
+
"email": "hkamran@hkamran.com",
|
|
11
|
+
"url": "https://hkamran.com"
|
|
12
|
+
},
|
|
13
|
+
"repository": {
|
|
14
|
+
"type": "git",
|
|
15
|
+
"url": "git+https://github.com/hkamran80/utilities-js.git",
|
|
16
|
+
"directory": "packages/assets"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"assets",
|
|
20
|
+
"site",
|
|
21
|
+
"website",
|
|
22
|
+
"move",
|
|
23
|
+
"files",
|
|
24
|
+
"filesystem",
|
|
25
|
+
"fs",
|
|
26
|
+
"folder",
|
|
27
|
+
"folders",
|
|
28
|
+
"json",
|
|
29
|
+
"css"
|
|
30
|
+
],
|
|
31
|
+
"license": "AGPL-3.0-or-later",
|
|
32
|
+
"bugs": {
|
|
33
|
+
"url": "https://github.com/hkamran80/utilities-js/issues?q=is%3Aopen+label%3A%22utility%3A+assets%22+sort%3Aupdated-desc"
|
|
34
|
+
},
|
|
35
|
+
"homepage": "https://github.com/hkamran80/utilities-js/tree/main/packages/assets#readme",
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@hkamran/prettier-config": "^1.1.1",
|
|
41
|
+
"eslint": "^8.36.0",
|
|
42
|
+
"eslint-config-prettier": "^8.7.0",
|
|
43
|
+
"prettier": "^2.8.4",
|
|
44
|
+
"typescript": "^4.6.2"
|
|
45
|
+
},
|
|
46
|
+
"prettier": "@hkamran/prettier-config",
|
|
47
|
+
"scripts": {
|
|
48
|
+
"lint": "eslint --ext .js,.ts --fix",
|
|
49
|
+
"format": "prettier . --write",
|
|
50
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
51
|
+
}
|
|
52
|
+
}
|