Skip to content

Commit cca16e2

Browse files
Pushing test for SFRA Watch
1 parent fd47601 commit cca16e2

File tree

3 files changed

+130
-35
lines changed

3 files changed

+130
-35
lines changed

commands/watch.js

Lines changed: 114 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const {exec} = require('child_process')
22
const argv = require('minimist')(process.argv.slice(2))
33
const chalk = require('chalk')
44
const chokidar = require('chokidar')
5+
const fs = require('fs')
56
const ipc = require('node-ipc')
67
const ora = require('ora')
78
const path = require('path')
@@ -16,6 +17,7 @@ module.exports = options => {
1617
let instance = argv['_'][2] || null
1718
let selected = null
1819
let errorMessage
20+
let recentFiles = []
1921

2022
const useLog = options.log
2123
const errorsOnly = options.errorsOnly
@@ -34,10 +36,18 @@ module.exports = options => {
3436
}
3537

3638
if (selected) {
37-
let spinner
39+
const text = `${chalk.bold('WATCHING')} ${chalk.cyan.bold(client)} ${chalk.magenta.bold(
40+
instance
41+
)} [Ctrl-C to Cancel]\n`
42+
const spinner = ora(text)
43+
const output = fn => {
44+
spinner.stop()
45+
fn()
46+
spinner.start()
47+
}
3848

3949
const watcher = chokidar.watch(selected.d, {
40-
ignored: [/[/\\]\./, '**/node_modules/**'],
50+
ignored: [/[/\\]\./, '**/node_modules/**', '**/bundle-analyzer.*'],
4151
ignoreInitial: true,
4252
persistent: true,
4353
awaitWriteFinish: true
@@ -59,32 +69,111 @@ module.exports = options => {
5969
})
6070
})
6171

72+
/**
73+
* Add support for SFRA
74+
* @param {string} ext File Extension
75+
* @param {string} dir Directory
76+
*/
77+
const compile = (ext, dir) => {
78+
const jsCompile = `cd ${dir}; ./node_modules/.bin/sgmf-scripts --compile js`
79+
const cssCompile = `cd ${dir}; ./node_modules/.bin/sgmf-scripts --compile css`
80+
81+
if (ext === 'js') {
82+
output(() =>
83+
console.log(
84+
`\n${chalk.bgGreen.white.bold(' SFRA ')} ${chalk.cyan.bold('Compiling')} ${chalk.magenta.bold(
85+
'JavaScript'
86+
)} ...\n`
87+
)
88+
)
89+
90+
exec(jsCompile, (err, data, stderr) => {
91+
if (err || stderr) {
92+
output(() => console.log(chalk.red.bold(`✖ Build Error: ${err} ${stderr}`)))
93+
}
94+
})
95+
} else if (ext === 'css' || ext === 'scss') {
96+
output(() =>
97+
console.log(
98+
`\n${chalk.bgGreen.white.bold(' SFRA ')} ${chalk.cyan.bold('Compiling')} ${chalk.magenta.bold('CSS')} ...\n`
99+
)
100+
)
101+
102+
exec(cssCompile, (err, data, stderr) => {
103+
if (err || stderr) {
104+
output(() => console.log(chalk.red.bold(`✖ SFRA Compile Error: ${err} ${stderr}`)))
105+
}
106+
})
107+
}
108+
}
109+
62110
const buildCheck = file => {
63-
if (Object.keys(selected.b).length > 0) {
64-
const checkPath = path.dirname(file).replace(path.normalize(selected.d), '')
65-
Object.keys(selected.b).map(build => {
66-
const builder = selected.b[build]
111+
if (recentFiles.indexOf(file) === -1) {
112+
recentFiles.push(file)
113+
114+
setTimeout(() => {
115+
let idx = recentFiles.indexOf(file)
116+
recentFiles.splice(idx, 1)
117+
}, 10000)
118+
119+
if (Object.keys(selected.b).length > 0) {
120+
const checkPath = path.dirname(file).replace(path.normalize(selected.d), '')
121+
Object.keys(selected.b).map(build => {
122+
const builder = selected.b[build]
123+
if (
124+
builder.enabled &&
125+
new RegExp(builder.watch.join('|')).test(checkPath) &&
126+
typeof builder.cmd.exec !== 'undefined' &&
127+
builder.cmd.exec.length > 0
128+
) {
129+
const cmd = builder.cmd.exec
130+
const building = build.split('_')
131+
132+
output(() =>
133+
console.log(
134+
`\n${chalk.bgGreen.white.bold(' BUILDING ')} ${chalk.cyan.bold(
135+
building[1]
136+
)} for cartridge ${chalk.magenta.bold(building[0])} ...\n\n`
137+
)
138+
)
139+
exec(cmd, (err, data, stderr) => {
140+
if (err || stderr) {
141+
output(() => console.log(chalk.red.bold(`✖ Build Error: ${err} ${stderr}`)))
142+
}
143+
})
144+
}
145+
})
146+
} else {
147+
const filePath = path.dirname(file)
148+
const ext = file.split('.').pop()
149+
const dirs = filePath.split('/')
150+
const length = path.normalize(selected.d).split('/').length
151+
152+
// Ignore file changes that are likely results from builds
153+
const ignoredPath = ['/css/', '/js/']
154+
155+
// Check current directory for WebPack
67156
if (
68-
builder.enabled &&
69-
new RegExp(builder.watch.join('|')).test(checkPath) &&
70-
typeof builder.cmd.exec !== 'undefined' &&
71-
builder.cmd.exec.length > 0
157+
fs.existsSync(path.join(filePath, 'webpack.config.js')) &&
158+
!new RegExp(ignoredPath.join('|')).test(file)
72159
) {
73-
const cmd = builder.cmd.exec
74-
const building = build.split('_')
75-
console.log(
76-
`\n${chalk.bgGreen.white.bold(' BUILDING ')} ${chalk.cyan.bold(
77-
building[1]
78-
)} for cartridge ${chalk.magenta.bold(building[0])} ...\n\n`
79-
)
80-
81-
exec(cmd, (err, data, stderr) => {
82-
if (err || stderr) {
83-
console.log(chalk.red.bold(`✖ Build Error: ${err} {stderr}`))
160+
compile(ext, filePath)
161+
} else {
162+
// Work our way backwards to look for WebPack until we get to project root
163+
for (var i = dirs.length; i >= length; i--) {
164+
dirs.pop()
165+
let curPath = dirs.join('/')
166+
167+
if (
168+
fs.existsSync(path.join(curPath, 'webpack.config.js')) &&
169+
!new RegExp(ignoredPath.join('|')).test(file)
170+
) {
171+
compile(ext, curPath)
172+
break
84173
}
85-
})
174+
}
86175
}
87-
})
176+
}
88177
}
89178
}
90179

@@ -107,7 +196,7 @@ module.exports = options => {
107196

108197
// @TODO: Watch for Removing Files
109198
watcher.on('unlink', file => {
110-
console.log(`${chalk.red('✗ REMOVING')} ${file.replace(selected.d, '.')}`)
199+
output(() => console.log(`${chalk.red('✗ REMOVING')} ${file.replace(selected.d, '.')}`))
111200
})
112201

113202
// Watch for Errors
@@ -135,7 +224,7 @@ module.exports = options => {
135224
if (useLog) {
136225
logger.log(errorMessage)
137226
} else {
138-
console.log(chalk.red.bold(`\n${errorMessage}`))
227+
output(() => console.log(chalk.red.bold(`\n${errorMessage}`)))
139228
}
140229
})
141230

@@ -163,10 +252,6 @@ module.exports = options => {
163252

164253
if (useLog) {
165254
logger.log(`Watching ${client} ${instance}`, true)
166-
} else {
167-
spinner = ora(
168-
`${chalk.bold('WATCHING')} ${chalk.cyan.bold(client)} ${chalk.magenta.bold(instance)} [Ctrl-C to Cancel]\n`
169-
).start()
170255
}
171256
})
172257
} else if (client && instance) {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sfcc-cli",
3-
"version": "1.1.1",
3+
"version": "1.2.0",
44
"description": "Command Line Interface for Salesforce Commerce Cloud Sandbox Development",
55
"homepage": "https://github.com/redvanworkshop/sfcc-cli#readme",
66
"license": "MIT",
@@ -14,9 +14,20 @@
1414
"keywords": [
1515
"cli",
1616
"salesforce",
17+
"sfcc",
1718
"commerce-cloud",
18-
"denadware",
19-
"sandbox"
19+
"demandware",
20+
"sandbox",
21+
"sfra",
22+
"watch",
23+
"build",
24+
"upload",
25+
"log",
26+
"remote",
27+
"search",
28+
"macos",
29+
"windows",
30+
"linux"
2031
],
2132
"contributors": [
2233
{
@@ -33,8 +44,7 @@
3344
"url": "https://github.com/redvanworkshop/sfcc-cli/issues"
3445
},
3546
"scripts": {
36-
"test": "eslint --ext .js ./ --fix && echo '\n【ツ】CODE PERFECTION !!!\n'",
37-
"precommit": "lint-staged"
47+
"test": "eslint --ext .js ./ --fix && echo '\n【ツ】CODE PERFECTION !!!\n'"
3848
},
3949
"lint-staged": {
4050
"*.js": [

0 commit comments

Comments
 (0)