Skip to content

Commit 5b76b8c

Browse files
committed
Script and config sample
1 parent ed50b9a commit 5b76b8c

File tree

2 files changed

+95
-40
lines changed

2 files changed

+95
-40
lines changed

bin/_sample.js

Lines changed: 87 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,37 @@ const fs = require('fs');
1010
const path = require('path');
1111
const chokidar = require('chokidar');
1212

13-
const cnsl = require(`${process.env.PWD}/node_modules/nycopportunity/pttrn/bin/util/console`);
14-
const global = require(`${process.env.PWD}/node_modules/nycopportunity/pttrn/config/global`);
15-
const alerts = require(`${process.env.PWD}/node_modules/nycopportunity/pttrn/config/alerts`);
16-
const config = resolve(`${process.env.PWD}/config/sample`);
13+
/**
14+
* Config file for this script
15+
*/
16+
17+
const config = require(`${process.env.PWD}/config/_sample`);
1718

1819
/**
19-
* Constants
20+
* @pttrn Dependencies
2021
*/
2122

22-
const SOURCE = path.join(global.base, global.src);
23-
const DIST = path.join(global.base, global.dist);
24-
const BASE_PATH = path.join(SOURCE, global.entry.views);
23+
const pttrn = `${process.env.PWD}/node_modules/@nycopportunity/patterns-framework`;
24+
const alerts = require(`${pttrn}/config/alerts`);
25+
const args = require(`${pttrn}/bin/util/args`).args;
26+
const cnsl = require(`${pttrn}/bin/util/console`);
2527

26-
const EXT = '.ext';
28+
/**
29+
* Constants
30+
*/
31+
32+
const SRC = config.src;
33+
const DIST = config.dist;
34+
const ENTRY = config.entry;
35+
const EXT = config.ext;
2736
const GLOBS = [
28-
`${SOURCE}/**/*${EXT}`
37+
`${config.src}/${config.dist}/**/*${config.ext}`
2938
];
3039

31-
/** Process CLI args */
32-
33-
const args = require(`${__dirname}/util/args`).args;
34-
const cnsl = require(`${__dirname}/util/console`);
35-
3640
/**
3741
* Our Chokidar Watcher
3842
*
39-
* @type {Source} https://github.com/paulmillr/chokidar
43+
* @source https://github.com/paulmillr/chokidar
4044
*/
4145
const watcher = chokidar.watch(GLOBS, {
4246
usePolling: false,
@@ -48,68 +52,113 @@ const watcher = chokidar.watch(GLOBS, {
4852
/**
4953
* Write file to the distribution folder
5054
*
51-
* @param {String} file The file source
52-
* @param {Object} data The data to pass to the file
55+
* @param {String} file The file source
56+
* @param {Object} data The data to pass to the file
5357
*
54-
* @return {undefined} The result of fs.writeFileSync()
58+
* @return {Undefined} The result of fs.writeFileSync()
5559
*/
5660
const write = async (file, data) => {
5761
try {
58-
let dist = file.replace(BASE_PATH, path.join(DIST, opts.svgs));
59-
let local = dist.replace(process.env.PWD, '.');
62+
let dist = file.replace(SRC, DIST);
6063

61-
if (!fs.existsSync(path.dirname(dist))){
62-
fs.mkdirSync(path.dirname(dist));
64+
if (!fs.existsSync(path.dirname(dist))) {
65+
fs.mkdirSync(path.dirname(dist), {recursive: true});
6366
}
6467

6568
let written = fs.writeFileSync(dist, data);
6669

67-
cnsl.describe(`Sample written to ${alerts.str.path(local)}`);
70+
cnsl.describe(`${alerts.success} ${alerts.str.path(file)} written to ${alerts.str.path(dist)}`);
6871

6972
return written;
7073
} catch (err) {
71-
cnsl.error(`Sample (write): ${err.stack}`);
74+
cnsl.error(`Failed (write): ${err.stack}`);
7275
}
7376
}
7477

7578
/**
76-
* Main script
79+
* A sample file read and replace method
80+
*
81+
* @param {String} file Path to the file
82+
*
83+
* @return {String} File contents
84+
*/
85+
const replace = async (file) => {
86+
let data = await fs.readFileSync(file, 'utf-8');
87+
88+
return data.replace('{{ source }}', 'distribution');
89+
};
90+
91+
/**
92+
* The main task bus for transforming contents of a source file
93+
*
94+
* @param {String} file Path to source file
95+
*
96+
* @return {String} Transformed data
7797
*/
7898
const main = async (file) => {
7999
try {
80-
// Main functionality. Call other async functions here.
81-
// let data = await ...(file)
100+
let data = await replace(file); // Do something with the file data here
82101

83102
await write(file, data);
84103

85104
return data;
86105
} catch (err) {
87-
cnsl.error(`Sample failed (main): ${err.stack}`);
106+
cnsl.error(`Failed (main): ${err.stack}`);
88107
}
89108
};
90109

91110
/**
92-
* Runner for the sample script
111+
* Read a specific file, if it is a directory read all of the files in it,
112+
* then, perform the main task on the file.
113+
*
114+
* @param {String} file A single file or directory to recursively walk
115+
*/
116+
const walk = async (file) => {
117+
if (file.includes(EXT)) {
118+
await main(file);
119+
} else {
120+
try {
121+
let files = fs.readdirSync(file, 'utf-8');
122+
123+
for (let i = files.length - 1; i >= 0; i--) {
124+
await walk(files[i], file);
125+
}
126+
} catch (err) {
127+
cnsl.error(`Failed (walk): ${err.stack}`);
128+
}
129+
}
130+
};
131+
132+
/**
133+
* Runner for the sample script. If the -w or --watch flag is passed it will
134+
* run the watcher method. If a single filename is passed it will run the
135+
* main task on the file.
136+
*
137+
* @type {Function}
93138
*/
94139
const run = async () => {
95140
if (args.watch) {
96141
try {
97142
watcher.on('change', async changed => {
98-
let local = changed.replace(process.env.PWD, '');
143+
cnsl.watching(`Detected change on ${alerts.str.path(changed)}`);
99144

100145
await main(changed);
101-
102-
cnsl.watching(`Detected change on ${alerts.str.path(`.${local}`)}`);
103146
});
104147

105-
cnsl.watching(`Sample watching ${alerts.str.ext(GLOBS.map(g => g.replace(process.env.PWD, '')).join(', '))}`);
148+
cnsl.watching(`Sample watching ${alerts.str.ext(GLOBS.join(', '))}`);
106149
} catch (err) {
107-
cnsl.error(`Sample (run): ${err.stack}`);
150+
cnsl.error(`Failed (run): ${err.stack}`);
108151
}
109152
} else {
110-
await main();
153+
let file = (process.argv[2]) ? process.argv[2] : false;
154+
155+
if (file) {
156+
await main(`${SRC}/${ENTRY}/${file}`);
157+
} else {
158+
cnsl.error(`Failed (run): A file needs to be passed as an argument or a directory with files needs to be present if not using the ${alerts.str.string('--watch')} flag.`);
159+
}
111160

112-
process.exit();
161+
process.exit(); // One-off commands must exit
113162
}
114163
};
115164

@@ -119,7 +168,5 @@ const run = async () => {
119168
* @type {Object}
120169
*/
121170
module.exports = {
122-
main: main,
123-
run: run,
124-
config: config
171+
run: run
125172
};

config/_sample.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const path = require('path');
2+
3+
module.exports = {
4+
'src': path.join(process.env.PWD, 'src'),
5+
'dist': path.join(process.env.PWD, 'dist'),
6+
'entry': 'entry',
7+
'ext': '.ext'
8+
};

0 commit comments

Comments
 (0)