-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
180 lines (163 loc) · 6.02 KB
/
index.js
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-disable prefer-destructuring */
// ==Headers==
// @Name: task
// @Description: 自动更新头部信息(Headers)
// @Version: 1.0.111
// @Author: dodying
// @Modified: 2023-04-02 19:50:20
// @Namespace: https://github.com/dodying/Nodejs
// @SupportURL: https://github.com/dodying/Nodejs/issues
// @Require: clipboardy,inquirer
// ==/Headers==
// usage: mode file
// mode: one of "userjs"/"nodejs"/"deno"/"bookmarklet"
// 设置
// 导入原生模块
const fs = require('fs');
const cp = require('child_process');
const path = require('path');
const clipboardy = require('clipboardy');
const replaceWithDict = require('../_lib/replaceWithDict');
// 导入第三方模块
// const inquirer = require('inquirer');
const _ = require('./config');
// Function
// const getVar = async name => {
// if (!name) return _;
// if (!(name in _)) {
// const prompt = await inquirer.prompt({
// type: 'input',
// name: 'var',
// message: `${name}:\t`,
// validate: value => value ? true : 'Please enter a valid string'
// });
// _[name] = prompt.var;
// }
// return _[name];
// };
const spawnSync = (...argsForSpwan) => new Promise((resolve) => {
const child = cp.spawn(...argsForSpwan);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
child.on('exit', (code) => {
let end;
if (code.toString() !== '0') {
end = 'error';
console.error(`Command:\t${argsForSpwan[0]}\nCommand Args:${argsForSpwan[1].map((i) => `{${i}}`).join(', ')}\nExit Code:\t${code.toString()}`);
} else {
end = true;
}
resolve(end);
});
});
// Main
async function init() {
const argv = process.argv.slice(2);
console.log(argv);
const mode = argv[0];
const item = argv[1];
if (mode === 'userjs') {
const content = fs.readFileSync(item, 'utf-8');
const eof = content.match('\r\n') ? '\r\n' : '\n';
const arr = content.split(eof);
const headerStart = '// ==UserScript==';
const headerEnd = '// ==/UserScript==';
const start = arr.indexOf(headerStart);
const end = arr.indexOf(headerEnd);
if (start < 0 || end < 0) return;
for (let i = start + 1; i < end; i++) {
if (!arr[i].match(/^(.*?)@(.*?)(\s+)(.*)$/)) continue;
const [, pre, key, space, value] = arr[i].match(/^(.*?)@(.*?)(\s+)(.*)$/);
_[`__script_${key}`] = value;
if (key === 'version') {
const version = value.split('.');
const temp = parseInt(version[version.length - 1], 10);
if (!Number.isNaN(temp)) version[version.length - 1] = version[version.length - 1].replace(temp, temp + 1);
arr[i] = `${pre}@${key}${space}${version.join('.')}`;
} else if (key === 'author') {
arr[i] = `${pre}@${key}${space}${_.author}`;
} else if (key === 'modified') {
arr[i] = `${pre}@${key}${space}${_.nowStr}`;
} else if (key === 'task-next-line') {
arr[i + 1] = replaceWithDict(value, _);
i = i + 1;
}
}
fs.writeFileSync(item, arr.join(eof));
} else if (mode === 'nodejs' || mode === 'deno') {
const content = fs.readFileSync(item, 'utf-8');
const eof = content.match('\r\n') ? '\r\n' : '\n';
const arr = content.split(eof);
const headerStart = '// ==Headers==';
const headerEnd = '// ==/Headers==';
const start = arr.indexOf(headerStart);
const end = arr.indexOf(headerEnd);
if (start >= 0 && end > start) {
for (let i = start + 1; i < end; i++) {
if (!arr[i].match(/^(.*?)@(.*?):(\s*)(.*?)$/)) continue;
const [, pre, key, space, value] = arr[i].match(/^(.*?)@(.*?):(\s*)(.*?)$/);
_[`__script_${key}`] = value;
if (key === 'Version') {
const version = value.split('.');
const temp = parseInt(version[version.length - 1], 10);
if (!Number.isNaN(temp)) version[version.length - 1] = version[version.length - 1].replace(temp, temp + 1);
arr[i] = `${pre}@${key}:${space}${version.join('.')}`;
} else if (key === 'Author') {
arr[i] = `${pre}@${key}:${space}${_.author}`;
} else if (key === 'Modified') {
arr[i] = `${pre}@${key}:${space}${_.nowStr}`;
} else if (key === 'Require') {
let libs = arr.map((i) => {
if (i.match(/require\(.*?\)/)) {
i = i.match(/require\((.*?)\)/)[1];
} else if (i.match(/^import .* from (.*?)(;|\n)/m)) {
i = i.match(/^import .* from (.*?)(;|\n)/m)[1];
} else {
return null;
}
if (i.match(/^("|')(.*)("|')$/)) {
i = i.match(/^("|')(.*)("|')$/)[2];
} else {
return null;
}
if (i.match(/\.\//)) {
return null;
} if (i.match('/')) {
const arr1 = i.split('/');
i = arr1[0].startsWith('@') ? arr1.slice(0, 2).join('/') : arr1[0];
}
if (_.nativeModule.includes(i) || i.match(/^node:[a-z_]+(\/|$)/)) {
return null;
}
return i;
}).filter((i) => i);
libs = Array.from(new Set(libs));
if (libs.length === 0) libs = ['null'];
arr[i] = `${pre}@${key}:${space}${libs.join(',')}`;
} else if (key === 'task-next-line') {
arr[i + 1] = replaceWithDict(value, _);
i = i + 1;
}
}
fs.writeFileSync(item, arr.join(eof));
}
if (mode === 'deno') {
// await spawnSync('deno', ['fmt', item]);
}
} else if (mode === 'bookmarklet') {
const { dir, name } = path.parse(item);
const minified = path.join(dir, `${name}.min.js`);
// 使用encodeURIComponent压缩
const content = fs.readFileSync(item);
const minifiedContent = `javascript:${encodeURIComponent(`(function (){${content}})()`)}`;
fs.writeFileSync(minified, minifiedContent);
clipboardy.writeSync(minifiedContent);
} else {
console.log(item);
}
}
init().then(() => {
}, (err) => {
console.error(err);
process.exit(err);
});