Browse Source

fix upgrade errors async

pull/1/head
Cian Butler 3 years ago
parent
commit
a6e8b6a80a
Signed by: butlerx GPG Key ID: B37CA765BAA89170
9 changed files with 102 additions and 120 deletions
  1. +2
    -2
      LICENSE
  2. +2
    -1
      package.json
  3. +3
    -17
      schema.json
  4. +5
    -8
      src/assets.js
  5. +14
    -19
      src/checkEventJson.js
  6. +5
    -12
      src/cli.js
  7. +56
    -58
      src/generate.js
  8. +5
    -0
      src/index.js
  9. +10
    -3
      yarn.lock

+ 2
- 2
LICENSE View File

@@ -1,9 +1,9 @@
MIT License

Copyright (c) <year> <copyright holders>
Copyright (c) 2017 Cian Butler

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 2
- 1
package.json View File

@@ -37,6 +37,7 @@
"fs-extra": "^4.0.1",
"glob-promise": "^3.1.0",
"handlebars": "^4.0.10",
"js-yaml": "^3.10.0",
"json-matter": "^1.0.2",
"jsonschema": "^1.1.1",
"lodash": "^4.17.4",
@@ -66,7 +67,7 @@
"eslint-plugin-promise": "3.5.0",
"eslint-plugin-react": "^7.1.0",
"eslint-plugin-standard": "3.0.1",
"flow-bin": "^0.47.0",
"flow-bin": "^0.59.0",
"husky": "^0.14.3",
"lint-staged": "^4.0.1",
"prettier-eslint-cli": "^4.1.1"


+ 3
- 17
schema.json View File

@@ -115,10 +115,7 @@
"default": "Somewhere in DCU"
}
},
"required": [
"id",
"map"
]
"required": ["id", "map"]
},
"description": {
"type": "string",
@@ -126,27 +123,16 @@
"default": "TBA"
}
},
"required": [
"name",
"by",
"time",
"place",
"description"
]
"required": ["name", "by", "time", "place", "description"]
}
}
},
"required": [
"day",
"description",
"events"
]
"required": ["day", "description", "events"]
}
}
},
"required": [
"title",
"template",
"location",
"start",
"end",


+ 5
- 8
src/assets.js View File

@@ -5,14 +5,11 @@ import sass from 'node-sass';
/**
* Copy static assets from theme to dist
**/
async function staticMove(source: string, dest: string, assets: Array<string>): Promise {
try {
assets.forEach(i => {
fs.copy(path.join(source, assets[i]), path.join(dest, assets[i]));
});
} catch (err) {
throw err;
}
function staticMove(source: string, dest: string, assets: Array<string>): Promise {
assets.forEach(folder => {
fs.copy(path.join(source, folder), path.join(dest, folder));
});
return Promise.resolve();
}

/**


+ 14
- 19
src/checkEventJson.js View File

@@ -7,33 +7,28 @@ import { validate as val } from 'jsonschema';
/**
* Check json object against schema
**/
const checkJson = (schema: ?{}, json: ?{}): Promise =>
new Promise((resolve, reject) => {
try {
val(json, schema, { throwError: true });
resolve();
} catch (err) {
reject(err);
}
});
const checkJson = (schema: ?{}): Function => async (json: ?{}): Promise => {
try {
delete json.__contet__; // eslint-disable-line no-underscore-dangle
val(json, schema, { throwError: true });
} catch (err) {
throw err;
}
};

/**
* Validate all json files obey the schema
**/
async function validate(schemaPath: string, source: string): Promise {
export default async function validate(schemaPath: string, source: string): Promise {
try {
const schema = await fs.readJson(schemaPath);
const files = await glob(path.join(source, '*.json'), { ignore: 'node_modules' });
console.log(files);
files.forEach(async i => {
const data = await fs.readFile(i, 'utf-8');
const file = fm.parse(data);
const json = file.attributes;
await checkJson(schema, json);
});
files.forEach(file =>
fs.readFile(file, 'utf-8').then(fm.parse).then(checkJson(schema)).catch(err => {
throw err;
}),
);
} catch (err) {
throw err;
}
}

export default { validate };

+ 5
- 12
src/cli.js View File

@@ -3,18 +3,13 @@ import commandLineArgs from 'command-line-args';
import generate from './generate';

function config(): Promise {
return new Promise((resolve, reject) => {
fs
.writeJson('./config.json', {
title : 'Amazing event',
static: ['js', 'fonts', 'images'],
})
.then(resolve('Config created'))
.catch(reject);
return fs.writeJson('./config.json', {
title : 'Amazing event',
static: ['js', 'fonts', 'images'],
});
}

async function init() {
export default async function init() {
const options = commandLineArgs([
{
name : 'init',
@@ -35,11 +30,9 @@ async function init() {
]);
console.log('theme created');
} else {
console.log(await generate());
await generate();
}
} catch (err) {
console.error(err);
}
}

export default init;

+ 56
- 58
src/generate.js View File

@@ -2,6 +2,7 @@ import Handlebars from 'handlebars';
import fs from 'fs-extra';
import path from 'path';
import requireAll from 'require-all';
import yaml from 'js-yaml';
import fm from 'json-matter';
import marked from 'marked';
import object from 'lodash/fp/object';
@@ -9,50 +10,44 @@ import checkJson from './checkEventJson';
import assets from './assets';

// Initialise variables
const config = fs.readJson(path.join(process.cwd(), 'config.json'));
const config = yaml.safeLoad(fs.readFileSync(path.join(process.cwd(), 'config.json'), 'utf8'));
const source = path.join(process.cwd(), config.source || 'source');
const outputDir = config.output
? path.join(process.cwd(), config.output.dir)
: path.join(process.cwd(), 'public');
const outputDir = path.join(process.cwd(), config.output ? config.output.dir : 'public');

// Load all helper files
const helpers = requireAll({
dirname: path.join(
process.cwd(),
config.theme || 'theme',
config.helper || 'helper',
'**',
'*.js',
),
recursive: true,
dirname : path.join(process.cwd(), config.theme || 'theme', config.helper || 'helper'),
excludeDirs: /^\.(git|svn)$/,
recursive : true,
});
Object.key(helpers).forEach(helper => {
Object.keys(helpers).forEach(helper => {
Handlebars.registerHelper(helpers[helper]);
});

/**
* Render pages with handle bar template
**/
async function render(templateFile: string, page: ?{}, url: string): Promise {
try {
const template = `${templateFile}.hbs`;
// Load template and compile
const filePath = path.join(
process.cwd(),
config.theme || 'theme',
config.template || 'templates',
template,
);
const output = Handlebars.compile(await fs.readFile(filePath, 'utf-8'))(page);
await fs.endureDir(outputDir);
// if home page skip else create page dir
const dir = url !== 'index' ? path.join(outputDir, url) : outputDir;
await fs.endureDir(dir);
await fs.writeFile(path.join(dir, 'index.html'), output, 'utf8');
return;
} catch (err) {
throw err;
}
function render(url: string): Function {
return async (page: ?{}): Promise => {
try {
const template = `${page.template || 'schedule'}.hbs`;
// Load template and compile
const filePath = path.join(
process.cwd(),
config.theme || 'theme',
config.template || 'templates',
template,
);
const output = Handlebars.compile(await fs.readFile(filePath, 'utf-8'))(page);
await fs.ensureDir(outputDir);
// if home page skip else create page dir
const dir = url !== 'index' ? path.join(outputDir, url) : outputDir;
await fs.ensureDir(dir);
await fs.writeFile(path.join(dir, 'index.html'), output, 'utf8');
} catch (err) {
throw err;
}
};
}

/**
@@ -61,32 +56,31 @@ async function render(templateFile: string, page: ?{}, url: string): Promise {
**/
async function generateMenu(): Promise<Array<{ title: string, url: string }>> {
try {
const menu = [];
menu.push({ title: 'Home', url: '' });
const files = await fs.readdir(source);
files.forEach(file => {
if (file.substring(0, file.lastIndexOf('.')) !== 'index') {
menu.push({
title: file.substring(0, file.lastIndexOf('.')),
url : file.substring(0, file.lastIndexOf('.')),
});
}
});
const filter = files.filter(file => file.substring(0, file.lastIndexOf('.')) !== 'index');
const menu = filter.map(file => ({
title: file.substring(0, file.lastIndexOf('.')),
url : file.substring(0, file.lastIndexOf('.')),
}));
menu.unshift({ title: 'Home', url: '' });
return menu;
} catch (err) {
throw err;
}
}

async function generate(configArgs: ?{}): Promise<string> {
export default async function generate(configArgs: ?{}): Promise<string> {
try {
object.merge(config, configArgs);
// Validate JSON against schema
const localSchema = '../schema.json';
const schemaPath = config.schema ? path.join(process.cwd(), config.schema) : localSchema;
await checkJson.validate(schemaPath, source);
await checkJson(
config.schema
? path.join(process.cwd(), config.schema)
: path.join(__dirname, '..', 'schema.json'),
source,
);
assets.staticMove(path.join(process.cwd(), config.theme || 'theme'), outputDir, config.static);
const css = config.output ? config.output.css : 'main.css';
const css = config.output && config.output.css ? config.output.css : 'main.css';
assets.scss(
`${process.cwd()}/${config.theme || 'theme'}/css/${config.css || 'main.scss'}`,
`${outputDir}/css/${css}`,
@@ -94,19 +88,23 @@ async function generate(configArgs: ?{}): Promise<string> {
// Generate Menu if not in Config
if (!config.menu) config.menu = await generateMenu();
const pages = await fs.readdir(source);
pages.forEach(async page => {
pages.forEach(page => {
const url = path.parse(page).name;
const data = await fs.readFile(path.join(source, page), 'utf-8');
const file = fm.parse(data);
file.site = config;
// render md in to html
file.body = marked(file.__content__); // eslint-disable-line no-underscore-dangle
render(file.template || 'schedule', file, url);
fs
.readFile(path.join(source, page), 'utf-8')
.then(fm.parse)
.then(file => {
file.site = config;
// render md in to html
file.body = marked(file.__content__); // eslint-disable-line no-underscore-dangle
return file;
})
.then(render(url))
.catch(err => {
throw err;
});
});
return 'Generated';
} catch (err) {
throw err;
}
}

export default generate;

+ 5
- 0
src/index.js View File

@@ -1,3 +1,8 @@
require('babel-core/register');
require('babel-polyfill');
require('./cli.js')();

process.on('unhandledRejection', (reason, p) => {
console.log('Unhandled Rejection at: Promise', p, 'reason:', reason);
// application specific logging, throwing an error, or other logic here
});

+ 10
- 3
yarn.lock View File

@@ -1508,9 +1508,9 @@ flat-cache@^1.2.1:
graceful-fs "^4.1.2"
write "^0.2.1"

flow-bin@^0.47.0:
version "0.47.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.47.0.tgz#a2a08ab3e0d1f1cb57d17e27b30b118b62fda367"
flow-bin@^0.59.0:
version "0.59.0"
resolved "https://registry.yarnpkg.com/flow-bin/-/flow-bin-0.59.0.tgz#8c151ee7f09f1deed9bf0b9d1f2e8ab9d470f1bb"

for-in@^1.0.1:
version "1.0.2"
@@ -2044,6 +2044,13 @@ js-tokens@^3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.1.tgz#08e9f132484a2c45a30907e9dc4d5567b7f114d7"

js-yaml@^3.10.0:
version "3.10.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc"
dependencies:
argparse "^1.0.7"
esprima "^4.0.0"

js-yaml@^3.4.3:
version "3.9.0"
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.9.0.tgz#4ffbbf25c2ac963b8299dc74da7e3740de1c18ce"


Loading…
Cancel
Save