-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from clawrock/dev
Gulp 4 update with extra features
- Loading branch information
Showing
15 changed files
with
268 additions
and
121 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
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,3 +1,3 @@ | ||
{ | ||
"presets": ["env"] | ||
"presets": ["@babel/preset-env"] | ||
} |
This file was deleted.
Oops, something went wrong.
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,19 +1,11 @@ | ||
import rimraf from 'gulp-rimraf'; | ||
import gulp from 'gulp'; | ||
import config from '../config'; | ||
import del from 'del'; | ||
import ThemeRegistry from '../utils/theme-registry'; | ||
|
||
export default function (done, theme) { | ||
const themeRegistry = new ThemeRegistry(); | ||
const themeConfig = themeRegistry.getTheme(theme); | ||
|
||
if (themeConfig) { | ||
return gulp.src(`${config.projectPath}pub/static/${themeConfig.area}/${themeConfig.name}`, {read: false}) | ||
.pipe(rimraf({force: true})); | ||
} | ||
|
||
return gulp.src([ | ||
config.projectPath + 'pub/static/*', | ||
'!' + config.projectPath + 'pub/static/.htaccess' | ||
], {read: false}).pipe(rimraf({force: true})); | ||
return del([ | ||
`${themeConfig.path}/**/*` | ||
]); | ||
} |
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
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,55 +1,89 @@ | ||
import gulp from 'gulp'; | ||
import AssetDeployer from '../utils/asset-deployer'; | ||
import ThemeRegistry from '../utils/theme-registry'; | ||
import config from '../config'; | ||
import { initSync } from '../utils/sync'; | ||
import { initSync, syncReload, isSyncEnabled } from '../utils/sync'; | ||
import path from 'path'; | ||
import gutil from 'gulp-util'; | ||
import log from 'fancy-log'; | ||
import chalk from 'chalk'; | ||
|
||
function runThemeTask(task, file) { | ||
const themeRegistry = new ThemeRegistry(); | ||
const theme = themeRegistry.getThemeKeyByFile(file); | ||
if (!theme) { | ||
gutil.log(chalk.red(`Theme task not found for this file!`)); | ||
} | ||
|
||
if (gulp.tasks[`${task}:${theme}`]) { | ||
gulp.start(`${task}:${theme}`); | ||
|
||
return; | ||
} | ||
gutil.log(chalk.red(`Task ${task}:${theme} not found!`)); | ||
} | ||
import del from 'del'; | ||
import { argv } from 'yargs'; | ||
|
||
function relativizePath(absolutePath) { | ||
return path.relative(process.cwd(), absolutePath); | ||
} | ||
|
||
export default function (done, theme) { | ||
const pubPath = `${config.projectPath}/pub/static`; | ||
|
||
initSync(); | ||
const assetDeployer = new AssetDeployer(theme); | ||
const themeRegistry = new ThemeRegistry(); | ||
const themeConfig = themeRegistry.getTheme(theme); | ||
const mainWatcher = gulp.watch(`${themeConfig.path}**/*.${themeConfig.dsl}`, gulp.series([`${themeConfig.dsl}:${theme}`])) | ||
.on('change', path => { | ||
log.info(chalk.white(`File ${relativizePath(path)} was changed`)); | ||
}); | ||
|
||
gulp.watch(`${themeConfig.sourcePath}**/*.${themeConfig.dsl}`) | ||
.on('add', path => { | ||
if (assetDeployer.isMagentoImportFile(path)) { | ||
mainWatcher.unwatch(`${themeConfig.path}**/*.${themeConfig.dsl}`); | ||
log.info(chalk.white(`File ${relativizePath(path)} detected as @magento_import, deploying source theme...`)); | ||
gulp.task(`exec:${theme}`)(() => { | ||
mainWatcher.add(`${themeConfig.path}**/*.${themeConfig.dsl}`); | ||
gulp.task(`${themeConfig.dsl}:${theme}`)(); | ||
}); | ||
return; | ||
} | ||
|
||
if (theme) { | ||
const themeRegistry = new ThemeRegistry(); | ||
const themeConfig = themeRegistry.getTheme(theme); | ||
gulp.watch(`${themeConfig.path}**/*.${themeConfig.dsl}`, [`${themeConfig.dsl}:${theme}`]).on('change', stream => { | ||
gutil.log(chalk.white(`File ${relativizePath(stream.path)} was changed`)); | ||
gulp.src(path).pipe(gulp.symlink(assetDeployer.resolveSymlinkPath(path))); | ||
log.info(chalk.white(`File ${relativizePath(path)} was created and linked pub`)); | ||
}).on('unlink', path => { | ||
mainWatcher.unwatch(`${themeConfig.path}**/*.${themeConfig.dsl}`); | ||
del([assetDeployer.resolveSymlinkPath(path)]).then(() => { | ||
log.info(chalk.white(`File ${relativizePath(path)} was deleted`)); | ||
if (assetDeployer.isMagentoImportFile(path)) { | ||
log.info(chalk.white(`File ${relativizePath(path)} detected as @magento_import, deploying source theme...`)); | ||
gulp.task(`exec:${theme}`)(() => { | ||
mainWatcher.add(`${themeConfig.path}**/*.${themeConfig.dsl}`); | ||
gulp.task(`${themeConfig.dsl}:${theme}`)(); | ||
}); | ||
return; | ||
} | ||
mainWatcher.add(`${themeConfig.path}**/*.${themeConfig.dsl}`); | ||
}); | ||
}); | ||
|
||
return; | ||
} | ||
const requireJsCallback = cb => { | ||
del([`${themeConfig.path}requirejs-config.js`]).then(() => { | ||
log.info(chalk.white(`Combined RequireJS configuration file removed from pub/static.`)); | ||
cb(); | ||
}); | ||
}; | ||
|
||
gulp.watch(`${pubPath}/**/*.less`).on('change', stream => { | ||
gutil.log(chalk.white(`File ${relativizePath(stream.path)} was changed`)); | ||
gulp.watch([ | ||
'app/code/**/requirejs-config.js', | ||
`${themeConfig.sourcePath}**/requirejs-config.js` | ||
], requireJsCallback); | ||
|
||
runThemeTask(`less`, stream.path); | ||
if (!isSyncEnabled()) { | ||
return; | ||
} | ||
|
||
}); | ||
const reload = cb => { | ||
syncReload(); | ||
cb(); | ||
}; | ||
|
||
gulp.watch(`${pubPath}/**/*.scss`).on('change', stream => { | ||
gutil.log(chalk.white(`File ${relativizePath(stream.path)} was changed`)); | ||
if (argv.phtml) { | ||
gulp.watch([ | ||
'app/code/**/*.phtml', | ||
`${themeConfig.sourcePath}**/*.phtml` | ||
], reload); | ||
} | ||
|
||
runThemeTask(`scss`, stream.path); | ||
}); | ||
if (argv.js) { | ||
gulp.watch([ | ||
'app/code/**/*.js', | ||
`${themeConfig.sourcePath}**/*.js` | ||
], reload); | ||
} | ||
} |
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,48 @@ | ||
import ThemeRegistry from './theme-registry'; | ||
import fs from 'fs'; | ||
|
||
class AssetDeployer { | ||
constructor(theme) { | ||
this.theme = (new ThemeRegistry()).getTheme(theme); | ||
this.magentoImport = {}; | ||
this.resolveMagentoImport(); | ||
} | ||
|
||
resolveSymlinkPath(sourceFile) { | ||
const destinationParts = sourceFile.split('/').splice(5).filter(part => part !== 'web'); | ||
destinationParts.pop(); | ||
|
||
return `${this.theme.path}${destinationParts.join('/')}`; | ||
} | ||
|
||
isMagentoImportFile(path) { | ||
return Object.keys(this.magentoImport).some(file => { | ||
return this.magentoImport[file].some(pattern => { | ||
return path.includes(pattern); | ||
}); | ||
}); | ||
} | ||
|
||
resolveMagentoImport() { | ||
this.theme.sourceFiles.forEach((file) => { | ||
const data = fs.readFileSync(file, 'UTF-8'); | ||
const importRe = new RegExp('\/\/@magento_import[^;]*', 'gm'); | ||
const result = data.match(importRe); | ||
if (!result) { | ||
return; | ||
} | ||
result.forEach((line) => { | ||
const lineRe = new RegExp('[\'"](.*)[\'"]'); | ||
const lineResult = line.match(lineRe); | ||
if (lineResult) { | ||
if (this.magentoImport[file] === undefined) { | ||
this.magentoImport[file] = []; | ||
} | ||
this.magentoImport[file].push(lineResult[1]); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default AssetDeployer; |
Oops, something went wrong.