Skip to content

Commit 8470eac

Browse files
committed
Apply eslint to gulpfile
1 parent 27551b7 commit 8470eac

File tree

7 files changed

+9045
-4339
lines changed

7 files changed

+9045
-4339
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ module.exports = {
88
],
99
parserOptions: {
1010
ecmaVersion: 9,
11-
sourceType: 'module'
11+
sourceType: 'module',
12+
tsconfigRootDir: __dirname,
13+
project: ['./tsconfig.json']
1214
},
1315
env: {
1416
browser: true,

gulpfile.babel.ts

Lines changed: 74 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,35 @@
1+
import { src, dest, series, parallel, watch as watchFiles, TaskFunction } from 'gulp';
12
import * as fs from 'fs';
2-
import gulp from 'gulp';
3-
import sourcemaps from 'gulp-sourcemaps';
3+
import del from 'del';
4+
import * as sourcemaps from 'gulp-sourcemaps';
45
import rename from 'gulp-rename';
5-
6-
import clean from 'gulp-clean';
76
import copy from 'gulp-copy';
87
import change from 'gulp-change';
9-
import archiver from 'gulp-archiver';
10-
118
import postcss from 'gulp-postcss';
9+
import terser from 'gulp-terser';
10+
import sass from 'gulp-sass';
11+
import libsass from 'sass';
1212
import cssnano from 'cssnano';
13+
import webpack from 'webpack-stream';
14+
import autoprefixer from 'autoprefixer';
15+
import archiver from 'gulp-archiver';
1316
import rtlcss from 'gulp-rtlcss';
1417
import cssimport from 'postcss-easy-import';
1518
import hexrgba from 'postcss-hexrgba';
16-
import autoprefixer from 'autoprefixer';
17-
18-
import webpack from 'webpack-stream';
19-
import terser from 'gulp-terser';
2019
import eslint from 'gulp-eslint';
21-
2220
import makepot from 'gulp-wp-pot';
2321
import gettext from 'gulp-gettext';
24-
25-
import phpcs from 'gulp-phpcs';
22+
import codesniffer from 'gulp-phpcs';
2623
import composer from 'gulp-composer';
2724

28-
import sass from 'gulp-sass';
29-
import libsass from 'sass';
30-
3125
import * as pkg from './package.json';
32-
import webpackConfig from './webpack.config';
26+
import { config as webpackConfig } from './webpack.config';
3327

3428
const src_files = {
3529
php: ['*.php', 'php/**/*.php'],
3630
js: ['js/**/*.ts', 'js/**/*.tsx', 'js/**/*.js', '!js/min/**/*'],
3731
css: ['css/*.scss', '!css/_*.scss'],
32+
all_css: ['css/**/*.scss'],
3833
dir_css: ['edit.css', 'manage.css'],
3934
};
4035

@@ -46,128 +41,120 @@ const dist_dirs = {
4641
const text_domain = pkg.name;
4742

4843
const postcss_processors = [
49-
cssimport({prefix: '_', extensions: ['.scss', '.css']}),
44+
cssimport({ prefix: '_', extensions: ['.scss', '.css'] }),
5045
hexrgba(),
5146
autoprefixer(),
52-
cssnano({preset: ['default', {discardComments: {removeAll: true}}]})
47+
cssnano({ preset: ['default', { discardComments: { removeAll: true } }] })
5348
];
5449

55-
gulp.task('css', done =>
56-
gulp.series(
57-
() => gulp.src(src_files.css)
58-
.pipe(sourcemaps.init())
59-
.pipe(sass(libsass)().on('error', sass(libsass).logError))
60-
.pipe(postcss(postcss_processors))
61-
.pipe(sourcemaps.write('.'))
62-
.pipe(gulp.dest(dist_dirs.css)),
63-
() => gulp.src(src_files.dir_css.map(file => dist_dirs.css + file))
64-
.pipe(rename({suffix: '-rtl'}))
65-
.pipe(sourcemaps.init())
66-
.pipe(rtlcss())
67-
.pipe(sourcemaps.write('.'))
68-
.pipe(gulp.dest(dist_dirs.css))
69-
)(done));
70-
71-
gulp.task('jslint', () =>
72-
gulp.src(src_files.js)
50+
export const css: TaskFunction = done => series(
51+
() => src(src_files.css)
52+
.pipe(sourcemaps.init())
53+
.pipe(sass(libsass)().on('error', sass(libsass).logError))
54+
.pipe(postcss(postcss_processors))
55+
.pipe(sourcemaps.write('.'))
56+
.pipe(dest(dist_dirs.css)),
57+
() => src(src_files.dir_css.map(file => dist_dirs.css + file))
58+
.pipe(rename({ suffix: '-rtl' }))
59+
.pipe(sourcemaps.init())
60+
.pipe(rtlcss())
61+
.pipe(sourcemaps.write('.'))
62+
.pipe(dest(dist_dirs.css))
63+
)(done)
64+
65+
export const jslint: TaskFunction = () =>
66+
src(src_files.js)
7367
.pipe(eslint())
7468
.pipe(eslint.format())
75-
.pipe(eslint.failAfterError()));
69+
.pipe(eslint.failAfterError())
7670

77-
gulp.task('js', gulp.series('jslint', () =>
78-
gulp.src(src_files.js)
71+
export const js: TaskFunction = series(jslint, () =>
72+
src(src_files.js)
7973
.pipe(webpack(webpackConfig))
8074
.pipe(sourcemaps.init())
8175
.pipe(terser())
8276
.pipe(sourcemaps.write('.'))
83-
.pipe(gulp.dest('js/min'))));
77+
.pipe(dest('js/min')))
8478

85-
gulp.task('makepot', () =>
86-
gulp.src(src_files.php)
79+
export const i18n = parallel([
80+
() => src(src_files.php)
8781
.pipe(makepot({
8882
domain: text_domain,
8983
package: 'Code Snippets',
9084
bugReport: 'https://github.com/sheabunge/code-snippets/issues',
9185
metadataFile: 'code-snippets.php',
9286
}))
93-
.pipe(gulp.dest(`languages/${text_domain}.pot`)));
94-
95-
gulp.task('gettext', () =>
96-
gulp.src('languages/*.po')
87+
.pipe(dest(`languages/${text_domain}.pot`)),
88+
() => src('languages/*.po')
9789
.pipe(gettext())
98-
.pipe(gulp.dest('languages')));
99-
100-
gulp.task('i18n', gulp.parallel(['makepot', 'gettext']));
90+
.pipe(dest('languages'))
91+
])
10192

102-
gulp.task('phpcs', () =>
103-
gulp.src(src_files.php)
104-
.pipe(phpcs({bin: 'vendor/bin/phpcs', showSniffCode: true}))
105-
.pipe(phpcs.reporter('log', {})));
93+
export const phpcs: TaskFunction = () =>
94+
src(src_files.php)
95+
.pipe(codesniffer({ bin: 'vendor/bin/phpcs', showSniffCode: true }))
96+
.pipe(codesniffer.reporter('log'))
10697

107-
gulp.task('vendor', () =>
108-
gulp.src('node_modules/codemirror/theme/*.css')
98+
export const vendor: TaskFunction = () =>
99+
src('node_modules/codemirror/theme/*.css')
109100
.pipe(postcss([cssnano()]))
110-
.pipe(gulp.dest(`${dist_dirs.css}editor-themes`)));
101+
.pipe(dest(`${dist_dirs.css}editor-themes`))
111102

112-
gulp.task('clean', () =>
113-
gulp.src([dist_dirs.css, dist_dirs.js], {read: false, allowEmpty: true})
114-
.pipe(clean()));
103+
export const clean: TaskFunction = () =>
104+
del([dist_dirs.css, dist_dirs.js])
115105

116106

117-
gulp.task('test', gulp.parallel('jslint', 'phpcs'));
107+
export const test = parallel(jslint, phpcs)
118108

119-
gulp.task('default', gulp.series('clean', gulp.parallel('vendor', 'css', 'js', 'i18n')));
109+
export const build = series(clean, parallel(vendor, css, js, i18n))
120110

121-
gulp.task('package', gulp.series(
122-
'default',
123-
'vendor',
111+
export default build
112+
113+
export const bundle: TaskFunction = series(
114+
build,
115+
vendor,
124116

125117
// Remove files from last run
126-
() => gulp.src(['dist', pkg.name, `${pkg.name}.*.zip`], {read: false, allowEmpty: true})
127-
.pipe(clean()),
118+
() => del(['dist', pkg.name, `${pkg.name}.*.zip`]),
128119

129120
// Remove composer dev dependencies
130-
() => composer({'no-dev': true}),
121+
() => composer('install', { 'no-dev': true }),
131122

132123
// Copy files into a new directory
133-
() => gulp.src([
124+
() => src([
134125
'code-snippets.php', 'uninstall.php', 'php/**/*', 'vendor/**/*',
135126
'readme.txt', 'license.txt', 'css/font/**/*', 'languages/**/*'
136127
])
137-
.pipe(copy(pkg.name, {})),
128+
.pipe(copy(pkg.name)),
138129

139130
// Copy minified scripts and stylesheets, while removing source map references
140-
() => gulp.src('css/min/**/*.css')
131+
() => src('css/min/**/*.css')
141132
.pipe(change(content => content.replace(/\/\*# sourceMappingURL=[\w.-]+\.map \*\/\s+$/, '')))
142-
.pipe(gulp.dest(`${pkg.name}/css/min`)),
133+
.pipe(dest(`${pkg.name}/css/min`)),
143134

144-
() => gulp.src('js/min/**/*.js')
135+
() => src('js/min/**/*.js')
145136
.pipe(change(content => content.replace(/\/\/# sourceMappingURL=[\w.-]+\.map\s+$/, '')))
146-
.pipe(gulp.dest(`${pkg.name}/js/min`)),
137+
.pipe(dest(`${pkg.name}/js/min`)),
147138

148139
// Create a zip archive
149-
() => gulp.src(`${pkg.name}/**/*`, {base: '.'})
140+
() => src(`${pkg.name}/**/*`, { base: '.' })
150141
.pipe(archiver(`${pkg.name}.${pkg.version}.zip`))
151-
.pipe(gulp.dest('.')),
142+
.pipe(dest('.')),
152143

153144
done => {
154145
// Reinstall dev dependencies
155146
composer();
156147

157148
// Rename the distribution directory to its proper name
158-
fs.rename(pkg.name, 'dist', err => {
159-
if (err) throw err;
149+
fs.rename(pkg.name, 'dist', error => {
150+
if (error) throw error;
160151
done();
161152
});
162153
}
163-
));
164-
165-
gulp.task('test', gulp.parallel('jslint', 'phpcs'));
166-
167-
gulp.task('default', gulp.series('clean', gulp.parallel('css', 'js', 'i18n', 'vendor')));
154+
)
168155

169-
gulp.task('watch', gulp.series('default', done => {
170-
gulp.watch('css/**/*.scss', gulp.series('css'));
171-
gulp.watch(src_files.js, gulp.series('js'));
156+
export const watch: TaskFunction = series(build, done => {
157+
watchFiles(src_files.all_css, css);
158+
watchFiles(src_files.js, js);
172159
done();
173-
}));
160+
})

js/modules.d.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
declare module 'gulp-archiver' {
2+
import { ThroughStream } from 'through';
3+
import { ArchiverOptions } from 'archiver';
4+
export default function (file: string, opts?: ArchiverOptions): ThroughStream
5+
}
6+
7+
declare module 'gulp-rtlcss' {
8+
import { ThroughStream } from 'through';
9+
import { ConfigOptions } from 'rtlcss';
10+
export default function (config?: ConfigOptions): ThroughStream
11+
}
12+
13+
declare module 'postcss-easy-import' {
14+
import { Plugin } from 'postcss';
15+
export default function (opts: {
16+
prefix: string | boolean
17+
extensions: string | string[]
18+
}): Plugin
19+
}
20+
21+
declare module 'postcss-hexrgba' {
22+
import { Plugin } from 'postcss';
23+
export default function (): Plugin
24+
}
25+
26+
declare module 'gulp-eslint' {
27+
import { Transform } from 'stream';
28+
export default gulpEslint
29+
30+
const gulpEslint: {
31+
(): Transform
32+
format(formatter?: string, output?: WritableStream): Transform
33+
formatEach(formatter?: string, output?: WritableStream): Transform
34+
failAfterError(): Transform
35+
}
36+
}
37+
38+
declare module 'gulp-wp-pot' {
39+
import { Transform } from 'stream';
40+
export default function (options?: {
41+
bugReport?: string
42+
commentKeyword?: string
43+
domain?: string
44+
destFile?: string
45+
headers?: boolean | Record<string, string>
46+
getTextFunctions?: Record<string, string>
47+
includePOTCreationDate?: boolean
48+
lastTranslator?: string
49+
metadataFile?: string
50+
noFilePaths?: boolean
51+
package?: string
52+
parser?: 'php' | 'js'
53+
parserOptions?: Record<string, unknown>
54+
relativeTo?: string
55+
src?: string | string[]
56+
globOpts?: Record<string, unknown>
57+
team?: string
58+
writeFile?: boolean
59+
ignoreTemplateNameHeader?: boolean
60+
}): Transform
61+
}
62+
63+
declare module 'gulp-gettext' {
64+
import { Transform } from 'stream';
65+
export default function (): Transform
66+
}
67+
68+
declare module 'gulp-phpcs' {
69+
import { Transform } from 'stream';
70+
const phpcs: {
71+
(options?: {
72+
bin?: string
73+
severity?: number
74+
warningSeverity?: number
75+
errorSeverity?: number
76+
standard?: string
77+
encoding?: string
78+
report?: string
79+
showSniffCode?: boolean
80+
sniffs?: string[]
81+
ignore?: string[]
82+
cwd?: string
83+
colors?: boolean
84+
}): Transform
85+
reporter(name: 'fail' | 'log' | 'file', options?: {
86+
failOnFirst?: boolean
87+
path?: string
88+
}): Transform
89+
}
90+
export default phpcs
91+
}
92+
93+
declare module 'gulp-composer' {
94+
import { Transform } from 'stream';
95+
export default function (cmd?: string, opts?: {
96+
'bin'?: string
97+
'self-install'?: boolean
98+
'async'?: boolean
99+
'ansi'?: boolean
100+
'working-dir'?: string
101+
} & Record<string, unknown>): Transform
102+
}

0 commit comments

Comments
 (0)