-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
118 lines (95 loc) · 3.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'use strict'
import type { Options } from './index.types'
export * from './index.types'
export * from './createAutoImportTask'
/*
Loosely based on the code used in Gulp Concat
https://github.com/gulp-community/gulp-concat/blob/master/index.js
*/
// npm
const through = require('through2')
const fileExists = require('file-exists')
const c = require('chalk')
// helpers
const err = require('./core/helpers/err')
const log = require('./core/helpers/log')
const join = require('./core/helpers/join')
const get_relative_path = require('./core/content-generators/get_relative_path')
const read_file = require('./core/file_manipulation/read_file')
const generate_content = require('./core/content-generators/generate_content')
const create_file = require('./core/file_manipulation/create_file')
const order_content = require('./core/content-generators/order_content')
const presets = require('./core/content-generators/preset-settings')
const dest_error = require('./core/error-messages/dest')
const format_error = require('./core/error-messages/format')
const fileName_error = require('./core/error-messages/fileName')
interface File {
isNull: () => boolean
isStream: () => boolean
stat?: {
mtime: number
}
}
type Done = () => void
const autoImports = function (opt: Options): any {
if (opt.preset) {
opt = Object.assign({}, presets[opt.preset], opt)
}
err(!opt.fileName, fileName_error)
err(!opt.format, format_error)
err(!opt.dest, dest_error)
let lastFile: File
let latestMod: number | undefined
const generatedFilePath = join([opt.dest, opt.fileName])
const relativePaths: Array<string> = []
function bufferContents(file: File, _enc: any, done: Done) {
// ignore empty files
if (file.isNull()) {
return done()
}
// gulp-auto-imports doesn't support streams
err(file.isStream(), 'Streaming is not supported')
// set latest file if not already set,
// or if the current file was modified more recently.
if (!latestMod || (file.stat && file.stat.mtime > latestMod)) {
lastFile = file
latestMod = file.stat && file.stat.mtime
}
relativePaths.push(get_relative_path(file, opt.dest))
done()
}
function endStream(done: Done) {
// no files passed in, no file goes out
if (!lastFile) {
return done()
}
const new_content = () =>
generate_content({ pathsArray: relativePaths, opt })
const generate_file = (content: string) => {
const newFile = create_file(lastFile, opt, content)
log(`Generated ${c.magenta(join([opt.dest, c.yellow(opt.fileName)]))}`)
this.push(newFile)
done()
}
fileExists(generatedFilePath, (error: any, exists: boolean) => {
err(error, error)
if (exists) {
read_file(generatedFilePath).then((oldContent: string) => {
const orderedContent = opt.retainOrder
? order_content({ oldContent, newPaths: relativePaths, opt })
: new_content()
if (orderedContent === oldContent) {
//Skip file generation
done()
} else {
generate_file(orderedContent)
}
})
} else {
generate_file(new_content())
}
})
}
return through.obj(bufferContents, endStream)
}
export default autoImports