Skip to content

Commit 67384e2

Browse files
committed
Closes #6 - fixed file overwrite bug
1 parent e961f8f commit 67384e2

File tree

6 files changed

+102
-55
lines changed

6 files changed

+102
-55
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.3.0] - 2020-09-10
11+
12+
### Added
13+
14+
- preflight resets `<button>` padding
15+
- preflight resets `<a>` text color and decoration styles
16+
- CSSMonster will throw an error when two files have the same name
17+
- `autoresolve` config value -- boolean
18+
19+
### Updated
20+
21+
- npm packages
22+
1023
## [0.2.1] - 2020-06-05
1124

1225
### Updated

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ Add the config file:
2727
```javascript
2828
module.exports = {
2929
sources: "./src", // Also accepts an array
30-
purgeCSS: {
31-
content: ["./templates/**/*.html"],
32-
},
3330
};
3431
```
3532

@@ -57,6 +54,7 @@ module.exports = {
5754
},
5855
blacklist: [],
5956
include: [], // Paths that will be included while compiling the SCSS
57+
autoresolve: false, // when true files with the same name are merged together
6058
};
6159
```
6260

cssmonster.js

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,10 @@ class CSSMonster {
5353
sources: [path.resolve(cwd, "src")],
5454
minify: true,
5555
purge: true,
56-
purgeCSS: {
57-
content: [path.resolve(cwd, "**/*.html")],
58-
},
56+
purgeCSS: null,
5957
blacklist: [],
6058
include: [],
59+
autoresolve: false,
6160
};
6261
this.run();
6362
}
@@ -67,7 +66,7 @@ class CSSMonster {
6766
if (fs.existsSync(this.tempDir)) {
6867
fs.rmdirSync(this.tempDir, { recursive: true });
6968
}
70-
fs.mkdir(this.tempDir, error => {
69+
fs.mkdir(this.tempDir, (error) => {
7170
if (error) {
7271
reject(error);
7372
}
@@ -179,12 +178,21 @@ class CSSMonster {
179178
}
180179
}
181180

181+
/** autoresolve */
182+
if (typeof config.autoresolve !== "undefined") {
183+
if (typeof config.autoresolve === "boolean") {
184+
this.config.autoresolve = config.autoresolve;
185+
} else {
186+
reject("Incorrect configuration: autoresolve must be a boolean.");
187+
}
188+
}
189+
182190
resolve();
183191
});
184192
}
185193

186194
removeIgnored(files) {
187-
return new Promise(resolve => {
195+
return new Promise((resolve) => {
188196
if (!files.length || !this.config.blacklist.length) {
189197
resolve(files);
190198
}
@@ -225,17 +233,39 @@ class CSSMonster {
225233
resolve(files);
226234
}
227235
const movedFiles = [];
236+
let count = 0;
228237
for (let i = 0; i < files.length; i++) {
229238
const filename = files[i].replace(/(.*\/)|(.*\\)/, "");
230-
fs.copyFile(files[i], `${this.tempDir}/${filename}`, error => {
231-
if (error) {
232-
reject(error);
233-
}
234-
movedFiles.push(`${this.tempDir}/${filename}`);
235-
if (movedFiles.length === files.length) {
236-
resolve(movedFiles);
239+
if (!fs.existsSync(`${this.tempDir}/${filename}`)) {
240+
fs.copyFile(files[i], `${this.tempDir}/${filename}`, (error) => {
241+
if (error) {
242+
reject(error);
243+
}
244+
movedFiles.push(`${this.tempDir}/${filename}`);
245+
count++;
246+
if (count === files.length) {
247+
resolve(movedFiles);
248+
}
249+
});
250+
} else {
251+
if (this.config.autoresolve) {
252+
const newFileData = fs.readFileSync(files[i]).toString();
253+
let tempFileData = fs.readFileSync(`${this.tempDir}/${filename}`).toString();
254+
tempFileData += "\n";
255+
tempFileData += newFileData;
256+
fs.writeFile(`${this.tempDir}/${filename}`, tempFileData, (error) => {
257+
if (error) {
258+
reject(error);
259+
}
260+
count++;
261+
if (count === files.length) {
262+
resolve(movedFiles);
263+
}
264+
});
265+
} else {
266+
reject(`Two files have the same name "${filename}" rename one of the files or enable the CSSMonster autoresolve setting.`);
237267
}
238-
});
268+
}
239269
}
240270
});
241271
}
@@ -253,7 +283,7 @@ class CSSMonster {
253283
if (fs.existsSync(`${this.tempDir}/normalize.css`)) {
254284
fs.unlinkSync(`${this.tempDir}/normalize.css`);
255285
}
256-
fs.writeFile(`${this.tempDir}/normalize.css`, data, error => {
286+
fs.writeFile(`${this.tempDir}/normalize.css`, data, (error) => {
257287
if (error) {
258288
reject(error);
259289
}
@@ -297,7 +327,7 @@ class CSSMonster {
297327
let fileName = result.stats.entry.replace(/(.*\/)|(.*\\)/, "").replace(/(.scss)$/g, "");
298328
if (fileName) {
299329
const newFile = `${this.tempDir}/${fileName}.css`;
300-
fs.writeFile(newFile, result.css.toString(), error => {
330+
fs.writeFile(newFile, result.css.toString(), (error) => {
301331
if (error) {
302332
reject("Something went wrong saving the file" + error);
303333
}
@@ -327,12 +357,12 @@ class CSSMonster {
327357
let minified = 0;
328358
for (let i = 0; i < files.length; i++) {
329359
minify(files[i])
330-
.then(css => {
331-
fs.unlink(files[i], error => {
360+
.then((css) => {
361+
fs.unlink(files[i], (error) => {
332362
if (error) {
333363
reject(error);
334364
}
335-
fs.writeFile(files[i], css, error => {
365+
fs.writeFile(files[i], css, (error) => {
336366
if (error) {
337367
reject(error);
338368
}
@@ -343,7 +373,7 @@ class CSSMonster {
343373
});
344374
});
345375
})
346-
.catch(error => {
376+
.catch((error) => {
347377
reject(error);
348378
});
349379
}
@@ -362,12 +392,12 @@ class CSSMonster {
362392
purgeCSSConfig.css = [`${this.tempDir}/**/*.css`];
363393
const purgecssResult = await new PurgeCSS().purge(purgeCSSConfig);
364394
const purgedFiles = [];
365-
purgecssResult.forEach(result => {
366-
fs.unlink(result.file, error => {
395+
purgecssResult.forEach((result) => {
396+
fs.unlink(result.file, (error) => {
367397
if (error) {
368398
throw error;
369399
}
370-
fs.writeFile(result.file, result.css, error => {
400+
fs.writeFile(result.file, result.css, (error) => {
371401
if (error) {
372402
throw error;
373403
}
@@ -385,7 +415,7 @@ class CSSMonster {
385415
if (fs.existsSync(this.config.outDir)) {
386416
fs.rmdirSync(this.config.outDir, { recursive: true });
387417
}
388-
fs.rename(this.tempDir, this.config.outDir, error => {
418+
fs.rename(this.tempDir, this.config.outDir, (error) => {
389419
if (error) {
390420
reject(error);
391421
}
@@ -395,7 +425,7 @@ class CSSMonster {
395425
}
396426

397427
cleanup() {
398-
return new Promise(resolve => {
428+
return new Promise((resolve) => {
399429
if (fs.existsSync(this.tempDir)) {
400430
fs.rmdirSync(this.tempDir, { recursive: true });
401431
}

package-lock.json

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cssmonster",
3-
"version": "0.2.1",
3+
"version": "0.3.0",
44
"description": "CSSMonster helps developers manage normalize.css, node-sass, and purgeCSS with ease.",
55
"main": "cssmonster.js",
66
"author": "Kyle Andrews",
@@ -20,9 +20,9 @@
2020
"minify": "^5.1.1",
2121
"node-sass": "^4.14.1",
2222
"normalize.css": "^8.0.1",
23-
"ora": "^4.0.4",
24-
"purgecss": "^2.2.1",
23+
"ora": "^4.1.1",
24+
"purgecss": "^2.3.0",
2525
"semver": "^7.3.2",
26-
"yargs": "^15.3.1"
26+
"yargs": "^15.4.1"
2727
}
2828
}

preflight.css

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,10 @@ button
6868
user-select: none;
6969
font-size: inherit;
7070
font-weight: inherit;
71+
padding: 0;
72+
}
73+
74+
a{
75+
text-decoration: none;
76+
color: inherit;
7177
}

0 commit comments

Comments
 (0)