This repository has been archived by the owner on Apr 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
189 additions
and
52 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
#! /usr/bin/env node | ||
"use strict"; | ||
const glob = require("glob"); | ||
const path = require("path"); | ||
const fs = require("fs-extra") | ||
|
||
const generateHtml = require("./generate-html"); | ||
const generateLocaleData = require("./generate-locale-data"); | ||
|
||
const PAGE_TITLE_KEY = "newtab-pageTitle"; | ||
const JS_FILENAME = "locale-data.js"; | ||
|
||
const defaults = { | ||
outputPath: path.join(__dirname, "../build"), | ||
i10nPath: path.join(__dirname, "../l10n"), | ||
staticPath: path.join(__dirname, "../www"), | ||
channels: ["nightly", "aurora", "beta", "release", "esr"] | ||
} | ||
|
||
class DirectoryGenerator { | ||
constructor(rawOptions) { | ||
const options = Object.assign({}, defaults, rawOptions); | ||
Object.keys(options).forEach(key => this[key] = options[key]); | ||
|
||
this.locales = glob.sync(`${this.i10nPath}/*/`).map(file => path.relative(this.i10nPath, file)); | ||
this.staticFiles = glob.sync(`${this.staticPath}/**/*`).map(file => path.relative(this.staticPath, file)); | ||
} | ||
|
||
outputFiles(options) { | ||
const js = options.js; | ||
const html = options.html; | ||
const dirPath = options.dirPath; | ||
|
||
const jsPath = path.join(dirPath, JS_FILENAME); | ||
const htmlPath = path.join(dirPath, "index.html"); | ||
|
||
this.staticFiles.forEach(file => { | ||
fs.copySync(path.join(this.staticPath, file), path.join(dirPath, file)); | ||
}); | ||
|
||
fs.outputFileSync(jsPath, js, "utf8"); | ||
fs.outputFileSync(htmlPath, html, "utf8"); | ||
} | ||
|
||
generateForLocale(locale) { | ||
const localeData = generateLocaleData(locale); | ||
const messages = localeData.messages; | ||
const js = localeData.fileString; | ||
const html = generateHtml({ | ||
title: messages[PAGE_TITLE_KEY] || "New Tab", | ||
locale, | ||
paths: { | ||
localeData: JS_FILENAME, | ||
js: "main.js", | ||
css: "main.css" | ||
// TODO prerender | ||
} | ||
}); | ||
|
||
this.channels.forEach(channel => { | ||
this.outputFiles({html, js, dirPath: path.join(this.outputPath, channel, locale)}); | ||
}); | ||
} | ||
|
||
run() { | ||
this.locales.forEach(this.generateForLocale.bind(this)); | ||
} | ||
|
||
} | ||
|
||
module.exports = DirectoryGenerator; | ||
|
||
if (require.main === module) { | ||
// called from command line | ||
const args = require("minimist")(process.argv.slice(2), {alias: { | ||
outputPath: ['o', 'output'], | ||
i10nPath: ['i', 'input'], | ||
staticPath: ['s', 'static'], | ||
channels: ['c'] | ||
}}); | ||
const generator = new DirectoryGenerator(args); | ||
console.log('Generating directories...'); | ||
generator.run(); | ||
console.log(`Finished generating directories for: | ||
${generator.channels.length} channels | ||
${generator.locales.length} locales`); | ||
} |
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 |
---|---|---|
@@ -1,35 +1,39 @@ | ||
#! /usr/bin/env node | ||
"use strict"; | ||
const defaults = { | ||
locale: "en-US", | ||
title: "New Tab", | ||
cssPath: "./main.css", | ||
jsPath: "./main.js", | ||
localeDataPath: "./locale-data.js" | ||
}; | ||
|
||
function template(options) { | ||
const locale = options.locale; | ||
const paths = options.paths; | ||
const title = options.title; | ||
function template(rawOptions) { | ||
const options = Object.assign({}, defaults, rawOptions || {}); | ||
return ` | ||
<!DOCTYPE html> | ||
<html lang="${locale}"> | ||
<html lang="${options.locale}"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width user-scalable=no" /> | ||
<link rel="stylesheet" href="${paths.css}"> | ||
<title>${title}</title> | ||
<link rel="stylesheet" href="${options.cssPath}"> | ||
<title>${options.title}</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="${paths.localeData}"></script> | ||
<script src="${paths.js}"></script> | ||
<script src="${options.localeDataPath}"></script> | ||
<script src="${options.jsPath}"></script> | ||
</body> | ||
</html> | ||
`; | ||
} | ||
}; | ||
|
||
process.stdout.write(template({ | ||
// todo: read from args | ||
locale: "en-US", | ||
title: "New Tab", | ||
paths: { | ||
css: "./main.css", | ||
js: "./main.js", | ||
localeData: "./locale-data.js" | ||
} | ||
})); | ||
module.exports = template; | ||
|
||
if (require.main === module) { | ||
// called from command line | ||
const args = require("minimist")(process.argv.slice(2), { | ||
alias: {locale: "l", title: "t"} | ||
}); | ||
process.stdout.write(template(args)); | ||
} |
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