Skip to content
Snippets Groups Projects
Commit db3f564d authored by Isabella Skořepová's avatar Isabella Skořepová
Browse files

Simplify by removing job system

parent aed1bdef
No related branches found
No related tags found
No related merge requests found
Pipeline #996 failed
......@@ -22,6 +22,7 @@ module.exports = {
'no-restricted-syntax': 0,
'no-underscore-dangle': 0,
'no-nested-ternary': 0,
'import/newline-after-import': 0,
},
globals: {
process: true,
......
#!/usr/bin/env node
const moment = require('moment')
const cli = require('cli')
const path = require('path')
const { spawnSync } = require('child_process')
const config = require('./sitegin/config')
cli.main(function(args, options) {
console.log(options)
config()
.then(function(obj) {
sitegin(obj.config)
})
.catch(function(e) {
console.log(e.stack)
process.exit()
})
})
function sitegin(config) {
const options = config.options
require('./sitegin/sitegin')({
watch: !options.noserver && !options.nowatch,
})
.then(function onLoad(jobs) {
const sass = require('node-sass')
const fs = require('fs')
// Main builder function
// If builder is not running - run it
// If it is running - schedule rerun after it finishes
let isRunning = false
let runAgain = false
let first = false
let doSync = function() {}
function run() {
const startTime = moment()
runAgain = false
if (isRunning) {
console.log(
'Generator is still running. Queing another run after it finishes.',
)
runAgain = true
return
}
console.log(
'================================================================================',
)
console.log('Running generator')
isRunning = true
jobs
.run('pipeline', jobs)
.then(function() {
isRunning = false
console.log(
'Generator finished in',
moment().diff(startTime, 'seconds'),
'seconds',
)
doSync()
if (!first) {
first = true
copyStaticFiles(
config.builddir,
config.staticDir,
config.themeDir,
)
}
if (runAgain) run()
})
.catch(function(e) {
isRunning = false
console.log(
'Generator crashed in',
moment().diff(startTime, 'seconds'),
'seconds',
)
if (runAgain) run()
if (e.stack) console.log(e.stack)
else console.log(e)
if (config.options.noserver) process.exit(13)
})
}
console.log('Sitegin successfully loaded')
run()
rendersass(config.builddir, config.themeDir)
if (!options.noserver) {
const sync = require('browser-sync').create()
doSync = function() {
sync.reload('*')
}
sync.init({
server: {
baseDir: config.builddir,
},
ui: {
port: options.port + 1,
},
port: options.port,
})
const chokidar = require('chokidar')
// article or theme reload
if (!options.nowatch) {
const watches = [
path.join(config.sourceDir, 'articles'),
config.themeDir,
]
console.log('Watching', watches, 'for changes')
chokidar
.watch(watches, { ignoreInitial: true })
.on('all', function(event, path) {
console.log('Content or theme changed. Rebuilding...')
console.log('Change event:', event, 'on path:', path)
run()
})
}
// sitegin reload
jobs.onReload(function() {
console.log('Sitegin reloaded')
run()
})
}
})
.catch(function(e) {
console.log(e.stack)
process.exit()
})
}
function copyStaticFiles(builddir, staticdir, themedir) {
// STATIC FILES
const fsextra = require('node-fs-extra')
fsextra.copy(
staticdir,
builddir,
function(file) {
return !(file.match('\\.git') || file.match('static/articles'))
},
function() {
console.log('copy static done')
},
)
fsextra.copy(`${staticdir}/articles`, `${builddir}/clanek`, function() {
console.log('copy static/articles done')
})
fsextra.copy(
`${themedir}/static`,
builddir,
function(file) {
return !file.match('\\.git')
},
function() {
console.log(`copy ${themedir}/static done`)
},
)
}
function rendersass(builddir, themedir) {
// SASS
const sass = require('node-sass')
const fs = require('fs')
const mkdirp = require('mkdirp')
sass.render({ file: `${themedir}/sass/style.scss` }, function(err, result) {
if (err === null) {
console.log('compiled sass')
mkdirp(`${builddir}/theme`, () => {
fs.writeFile(`${builddir}/theme/style.css`, result.css, () => {})
})
} else console.log('error ', err)
})
}
;(async () => {
const opts = await config()
if (opts.noserver) {
require('sitegin/index.js')
} else {
opts.spawnSync(
process.argv[0],
['./node_modules/.bin/nodemon', 'sitegin/index.js'],
{ stdio: ['inherit', 'inherit', 'inherit'] },
)
}
})()
{
"verbose": true,
"ignore": ["build-debug"]
}
\ No newline at end of file
This diff is collapsed.
......@@ -22,6 +22,7 @@
"node-fs-extra": "^0.8.2",
"node-sass": "^4.13.0",
"nodegit": ">=0.26.2",
"nodemon": "^1.19.4",
"nunjucks": "^3.2.0",
"nunjucks-date-filter": "^0.1.1",
"syntax-error": "^1.4.0",
......
const path = require('path')
const url = require('url')
const youtube = require('./utils/youtube.js')
const jobs = require('./jobs')
const { rewriteURL } = require('./urls')
const imageResizer = require('./imageResizer')
const generateImage = (file, targetDir, width, height) =>
jobs.run('imageResizer', file, targetDir, width, height)
imageResizer(file, targetDir, width, height)
function fromEntries(entries) {
return [...entries].reduce((o, [k,v]) => {
return [...entries].reduce((o, [k, v]) => {
o[k] = v
return o
}, {})
......
const moment = require('moment')
const cli = require('cli')
const path = require('path')
const config = require('./config')
cli.main(function(args, options) {
console.log(options)
config()
.then(function(obj) {
sitegin(obj.config)
})
.catch(function(e) {
console.log(e.stack)
process.exit()
})
})
function sitegin(config) {
const options = config.options
const pipeline = require('./sitegin')({
watch: !options.noserver && !options.nowatch,
})
const sass = require('node-sass')
const fs = require('fs')
// Main builder function
// If builder is not running - run it
// If it is running - schedule rerun after it finishes
let isRunning = false
let runAgain = false
let first = false
let doSync = function() {}
function run() {
const startTime = moment()
runAgain = false
if (isRunning) {
console.log(
'Generator is still running. Queing another run after it finishes.',
)
runAgain = true
return
}
console.log(
'================================================================================',
)
console.log('Running generator')
isRunning = true
pipeline()
.then(function() {
isRunning = false
console.log(
'Generator finished in',
moment().diff(startTime, 'seconds'),
'seconds',
)
doSync()
if (!first) {
first = true
copyStaticFiles(config.builddir, config.staticDir, config.themeDir)
}
if (runAgain) run()
})
.catch(function(e) {
isRunning = false
console.log(
'Generator crashed in',
moment().diff(startTime, 'seconds'),
'seconds',
)
if (runAgain) run()
if (e.stack) console.log(e.stack)
else console.log(e)
if (config.options.noserver) process.exit(13)
})
}
console.log('Sitegin successfully loaded')
run()
rendersass(config.builddir, config.themeDir)
if (!options.noserver) {
const sync = require('browser-sync').create()
doSync = function() {
sync.reload('*')
}
sync.init({
server: {
baseDir: config.builddir,
},
ui: {
port: options.port + 1,
},
port: options.port,
})
const chokidar = require('chokidar')
// article or theme reload
if (!options.nowatch) {
const watches = [path.join(config.sourceDir, 'articles'), config.themeDir]
console.log('Watching', watches, 'for changes')
chokidar
.watch(watches, { ignoreInitial: true })
.on('all', function(event, path) {
console.log('Content or theme changed. Rebuilding...')
console.log('Change event:', event, 'on path:', path)
run()
})
}
}
}
function copyStaticFiles(builddir, staticdir, themedir) {
// STATIC FILES
const fsextra = require('node-fs-extra')
fsextra.copy(
staticdir,
builddir,
function(file) {
return !(file.match('\\.git') || file.match('static/articles'))
},
function() {
console.log('copy static done')
},
)
fsextra.copy(`${staticdir}/articles`, `${builddir}/clanek`, function() {
console.log('copy static/articles done')
})
fsextra.copy(
`${themedir}/static`,
builddir,
function(file) {
return !file.match('\\.git')
},
function() {
console.log(`copy ${themedir}/static done`)
},
)
}
function rendersass(builddir, themedir) {
// SASS
const sass = require('node-sass')
const fs = require('fs')
const mkdirp = require('mkdirp')
sass.render({ file: `${themedir}/sass/style.scss` }, function(err, result) {
if (err === null) {
console.log('compiled sass')
mkdirp(`${builddir}/theme`, () => {
fs.writeFile(`${builddir}/theme/style.css`, result.css, () => {})
})
} else console.log('error ', err)
})
}
const syntaxError = require('syntax-error')
const fs = require('fs')
const chokidar = require('chokidar')
const jobList = {}
let watchers = []
function JobError(message, name) {
Error.captureStackTrace(this, JobError)
this.name = `${name}Error`
this.message = message
return this
}
let onReload = () => {}
const requireError = (e, jobName, module, reject) => {
if (e instanceof SyntaxError) {
console.log(`${module}: ${e}`)
const resolved = require.resolve(module)
fs.readFile(resolved, 'utf8', (err, content) => {
console.log(syntaxError(content, resolved))
reject(
new JobError(
`Failed to register job ${jobName} (SyntaxError)`,
'Require',
),
)
})
} else {
if (e.code === 'MODULE_NOT_FOUND') console.log(e.toString())
reject(new JobError(`Failed to register job ${jobName} (${e})`, 'Require'))
}
}
const jobs = {
register(jobName, module, watch = true) {
return new Promise((resolve, reject) => {
if (jobList[jobName] !== undefined) {
Promise.reject(
new JobError(
`Job ${jobName} is already registered`,
'JobAlreadyRegistered',
),
)
return
}
let f
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
f = require(module)
} catch (e) {
requireError(e, jobName, module, reject)
return
}
if (typeof f !== 'function')
throw new JobError(
`Module "${module}" for job "${jobName}" does not export function`,
)
jobList[jobName] = {
f,
module,
}
if (watch) jobs.watch(jobName)
resolve()
})
},
reload(jobName) {
const { module } = jobList[jobName]
delete require.cache[require.resolve(module)]
return new Promise((resolve, reject) => {
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
const f = require(module)
if (typeof f === 'function') {
const job = {
f,
module,
}
delete jobList[jobName]
jobList[jobName] = job
resolve()
} else {
console.log(`Error reloading job ${jobName} retry in 500ms`)
setTimeout(() => {
resolve(jobs.reload(jobName))
}, 500)
}
} catch (e) {
console.log(e)
requireError(e, jobName, module, reject)
}
})
},
// registerMultiple(
// {watch: true},
// ['jobName','./module'],['jobName2','./module2']
// )
registerMultiple({ watch = true }, ...args) {
const regs = []
for (const job of args) {
regs.push(jobs.register(job[0], job[1], watch))
}
return Promise.all(regs)
},
run(jobName, ...args) {
if (jobList[jobName] === undefined) {
throw new JobError(`Job ${jobName} is not registered`, 'JobNotRegistered')
}
try {
return jobList[jobName].f(...args)
} catch (e) {
console.log('Error running job', jobName)
console.log(jobList[jobName])
throw e
}
},
runSequence(...args) {
let prom
console.log('Running', args)
const jobSequence = args
jobSequence.forEach(job => {
if (prom === undefined) {
prom = jobs.run(job)
} else {
prom = prom.then((...a) => {
a.splice(0, 0, job)
return jobs.run(...a)
})
}
})
return prom
},
watch(jobName) {
const { module } = jobList[jobName]
const file = require.resolve(module)
const w = chokidar.watch(file)
w.on('change', () => {
jobs.reload(jobName).then(onReload)
})
watchers.push(w)
},
onReload(f) {
onReload = f
},
close() {
watchers.forEach(w => {
w.close()
console.log(w.getWatched())
})
watchers = []
},
}
module.exports = jobs
module.exports.jobList = jobList
......@@ -6,7 +6,8 @@ const marked = require('marked')
const renderer = new marked.Renderer()
const highlightjs = require('highlight.js')
const jobs = require('./jobs')
const image = require('./image')
const toURL = require('./toURL')
let lang
......@@ -35,10 +36,8 @@ const opts = {
const parser = new marked.Parser(opts)
const lexer = new marked.Lexer(opts)
const toURL = url => jobs.run('toURL', url)
const renderImage = (href, title, text, curFilename, cfg) =>
jobs.run('image', href, title, text, curFilename, cfg)
image(href, title, text, curFilename, cfg)
renderer.heading = (text, level, raw) =>
`<h${level + 2} id="${toURL(raw)}">${text}</h${level + 2}>\n`
......
/*
* This jobs just runs all job in the right order
*/
module.exports = jobs =>
jobs.runSequence(
'resetJobs',
'config',
'readFiles',
'parseHugo',
'parseRedirects',
'markdown',
'urls',
'sitemap',
'tags',
'theme',
'writeFiles',
'print',
)
const jobs = require('./jobs')
module.exports = function resetJobs() {
for (const job of Object.values(jobs.jobList)) {
if (job.f.reset) {
job.f.reset()
}
}
return Promise.resolve()
}
const jobs = require('./jobs')
const markdown = require('./markdown')
const parseHugo = require('./parseHugo')
const print = require('./print')
const readFiles = require('./readFiles')
const tags = require('./tags')
const theme = require('./theme')
const urls = require('./urls')
const writeFiles = require('./writeFiles')
const sitemap = require('./sitemap')
const parseRedirects = require('./parseRedirects')
const configI = require('./config')
module.exports = config =>
jobs
.registerMultiple(
config,
['config', './config'],
['markdown', './markdown'],
['parseHugo', './parseHugo'],
['print', './print'],
['readFiles', './readFiles'],
['toURL', './toURL'],
['tags', './tags'],
['nunjucks', './nunjucks'],
['theme', './theme'],
['urls', './urls'],
['writeFiles', './writeFiles'],
['resetJobs', './resetJobs'],
['sitemap', './sitemap'],
['parseRedirects', './parseRedirects'],
['image', './image'],
['imageResizer', './imageResizer'],
['pipeline', './pipeline'],
)
.then(() => jobs)
module.exports = config => {
return () =>
Promise.resolve(config)
.then(configI)
.then(readFiles)
.then(parseHugo)
.then(parseRedirects)
.then(markdown)
.then(urls)
.then(sitemap)
.then(tags)
.then(theme)
.then(writeFiles)
.then(print)
}
const jobs = require('./jobs')
const toURL = url => jobs.run('toURL', url)
const toURL = require('./toURL')
module.exports = function tagsJob(obj) {
console.log('Build step: Tags')
......
const jobs = require('./jobs')
const nunjucks = require('./nunjucks')
module.exports = function theme(_obj) {
console.log('Build step: Theme')
......@@ -11,8 +11,7 @@ module.exports = function theme(_obj) {
const runJob = (obj, type) => {
todo += 1
jobs
.run('nunjucks', obj, type, _obj.config)
nunjucks(obj, type, _obj.config)
.then(data => {
// eslint-disable-next-line no-param-reassign
if (!obj.rendered) obj.rendered = []
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment