-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from kristianheljas/dev
Publish to NPM
- Loading branch information
Showing
76 changed files
with
3,814 additions
and
494 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 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 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 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 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 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 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 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,68 @@ | ||
/* eslint-disable no-param-reassign */ | ||
import ejs from 'ejs'; | ||
import fs from 'fs'; | ||
import { join } from 'path'; | ||
import PluginError from 'plugin-error'; | ||
import { Transform } from 'stream'; | ||
import through, { TransformFunction } from 'through2'; | ||
import Vinyl from 'vinyl'; | ||
import { PACKAGE_NAME, PACKAGE_VERSION } from '../../src/core/constants'; | ||
|
||
const { stat, readFile } = fs.promises; | ||
|
||
const exists = async (path: string): Promise<boolean> => | ||
stat(path) | ||
.then(() => true) | ||
.catch(() => false); | ||
|
||
const transform: TransformFunction = async (file: Vinyl, encoding, callback) => { | ||
if (!file.isBuffer()) { | ||
callback(new PluginError('Only buffers are supported for ejs compilation!', __filename)); | ||
return; | ||
} | ||
|
||
file.extname = '.html'; | ||
|
||
// If html file already exists, skip everything else | ||
if (await exists(file.path)) { | ||
file.contents = await readFile(file.path); | ||
callback(null, file); | ||
return; | ||
} | ||
|
||
const { cwd, base, dirname, stem } = file; | ||
const viewsPath = join(cwd, base, 'views'); | ||
const editorViewsPath = join(cwd, base, 'editor'); | ||
|
||
const nodePath = join(dirname, 'node.ts'); | ||
const node = require(nodePath).default; // eslint-disable-line | ||
|
||
const templatePath = join(dirname, `${stem}.ejs`); | ||
|
||
const templateData: ejs.Data = { | ||
PACKAGE_NAME, | ||
PACKAGE_VERSION, | ||
env: process.env, | ||
node, | ||
}; | ||
|
||
const templateOptions: ejs.Options = { | ||
views: [dirname, editorViewsPath, viewsPath], | ||
}; | ||
|
||
try { | ||
if (await exists(templatePath)) { | ||
file.contents = await ejs.renderFile(templatePath, templateData, templateOptions).then(Buffer.from); | ||
callback(null, file); | ||
return; | ||
} | ||
|
||
const defaultTemplate = join(viewsPath, 'default-index.ejs'); | ||
file.contents = await ejs.renderFile(defaultTemplate, templateData, templateOptions).then(Buffer.from); | ||
callback(null, file); | ||
} catch (err) { | ||
callback(new PluginError('Something went wrong when compiling template!', err)); | ||
} | ||
}; | ||
|
||
export default (): Transform => through.obj(transform); |
Oops, something went wrong.