Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using 2 main classes instead of functions
- Loading branch information
1 parent
f919ef4
commit 3fcb0c8
Showing
10 changed files
with
276 additions
and
212 deletions.
There are no files selected for viewing
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 was deleted.
Oops, something went wrong.
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const fs = require("fs"); | ||
const chalk = require("chalk"); | ||
|
||
module.exports = class ProduceFile { | ||
constructor(results, path, ext) { | ||
this.results = results; | ||
this.path = path; | ||
this.ext = ext; | ||
} | ||
|
||
createFile(data, styleSheetLink, folder) { | ||
// read file and return an array of strings | ||
if (!data) return; // nothing in the array | ||
|
||
// generate dom | ||
const dom = this.createHtmlFile(data, styleSheetLink); | ||
try { | ||
// remove full path first and then extension | ||
const filename = this.path | ||
.replace(/^.*[\\\/]/, "") | ||
.replace(/\.[^/.]+$/, ""); | ||
|
||
// write the html to the dist folder | ||
fs.writeFileSync(`${folder}/${filename}.html`, dom); | ||
|
||
console.log(chalk.yellow("Convert to html successfully")); | ||
} catch (err) { | ||
console.log(chalk.yellow("Can not convert file")); | ||
return process.exit(1); | ||
} | ||
} | ||
|
||
// createFiles(styleSheetLink, folder) { | ||
// this.results.forEach((file) => { | ||
// this.createFile(file, styleSheetLink, folder); | ||
// }); | ||
// } | ||
|
||
createHtmlFile(data, styleSheetLink) { | ||
let dom = ""; | ||
data.forEach((e) => { | ||
if (e !== "") { | ||
dom += `<p>${e}</p>`; | ||
} else { | ||
dom += "\n\n"; | ||
} | ||
}); | ||
const result = ` | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<title>Filename</title> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
${ | ||
styleSheetLink | ||
? `<link rel='stylesheet' href='${styleSheetLink}'>` | ||
: "" | ||
} | ||
</head> | ||
<body> | ||
${dom} | ||
</body> | ||
</html> | ||
`; | ||
|
||
return result; | ||
} | ||
|
||
produce(styleSheetLink = "", folder) { | ||
this.createFile(this.results, styleSheetLink, folder); | ||
} | ||
}; |
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,24 @@ | ||
const fs = require("fs"); | ||
|
||
module.exports = class ProduceFolder { | ||
constructor() { | ||
this.path = `${process.cwd()}/dist`; | ||
this.createFolder(); | ||
} | ||
|
||
createFolder() { | ||
this.removeDir(); | ||
|
||
// create new dir | ||
fs.mkdirSync(this.path); | ||
} | ||
|
||
getPath() { | ||
return this.path; | ||
} | ||
|
||
removeDir() { | ||
// remove dir | ||
fs.rmdirSync(this.path, { recursive: true }); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.