This repository has been archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextension.js
202 lines (178 loc) · 6.98 KB
/
extension.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function activate(context) {
"use strict";
var vscode = require('vscode'),
builder = require('./libs/NicePageBuilder.js'),
izFS = require('./libs/izFS.js'),
com = vscode.commands.registerCommand('extension.nicePageBuilder',
/* settings.json
{
"nicePageBuilder.tasks" : [{
"htmlRoot" : { "path" : "src", "exclude" : "" },
"jsonList" : [
{ "path" : "src/jsons/list.json", "name" : "list" }, {} ...
],
"output" : "out"
}]
} */
function(){
var ws = vscode.workspace,
fs = new izFS( ws.rootPath ),
config = ws.getConfiguration('nicePageBuilder'),
tasks, currentTask, currentTarget, total, progress = 0, iterator, imports,
created = 0, skiped = 0;
if( !ws.rootPath ){
vscode.window.showErrorMessage('Use of mainFile requires a folder to be opened');
return;
};
if( !config.tasks ){
vscode.window.showErrorMessage('(T-T) Not fonund "nicePageBuilder.tasks" at settings.json.');
return;
};
try {
tasks = JSON.parse(JSON.stringify(config.tasks)); // deep copy
total = tasks.length;
} catch(o_O){
vscode.window.showErrorMessage( '(T-T) ' + o_O );
return;
};
ws.saveAll();
startTask();
// 1. Start Task, find .htm(l) files
function startTask(){
builder.reset();
if( progress < total ){
currentTask = tasks.shift();
if( currentTarget = currentTask.htmlRoot ){
++progress;
fs.find({
rootPath : currentTarget.rootPath,
include : currentTarget.include || createPath( currentTarget.rootPath, '**/*.htm*' ),
exclude : currentTarget.exclude,
getText : true
}, findFileDispatcher );
} else {
vscode.window.showErrorMessage( '(T-T) Not fonund "htmlRoot"!' );
};
} else {
vscode.window.setStatusBarMessage( '[' + progress + '/' + total + '] (^-^) All Tasks complete!' );
};
};
function findFileDispatcher( ite ){
var res;
switch( ite.type ){
case 'findFileSuccess' :
case 'readFileSuccess' :
try {
res = builder.readHTML(
'/' + ite.path.substr( createPath( currentTarget.rootPath, '' ).length ),
ite.data, ite.stats.birthtime.getTime(), ite.stats.mtime.getTime() );
} catch( o_O ){
vscode.window.showErrorMessage( '(T-T) ' + o_O );
return;
};
vscode.window.setStatusBarMessage( '[' + progress + '/' + total + '] read HTML [' + ite.index + '/' + ite.length + ']' );
if( res && res.importFiles && res.importFiles.length ){// array.<URLString>
if( !imports ){
imports = [];
iterator = ite;
};
while( res.importFiles.length ) imports.push( res.importFiles.shift() );
};
if( imports ){
importFiles();
} else {
ite.next();
};
break;
case 'findFileError' :
case 'readFileError' :
vscode.window.showErrorMessage( '(T-T) ' + ite.error );
( iterator || ite ).kill();
break;
case 'findFileComplete' :
startJsonTask();
break;
};
};
function importFiles(){
if( imports.length ){
fs.read({
path : createPath( currentTarget.rootPath, imports.shift() ),
getText : true
}, findFileDispatcher );
} else {
iterator.next();
imports = iterator = null;
};
};
function startJsonTask(){
currentTarget = currentTask.jsonList && currentTask.jsonList.shift();
if( currentTarget ){
fs.read({
path : currentTarget.path,
getText : true
}, findJsonDispatcher );
} else {
build();
};
};
function findJsonDispatcher( ite ){
switch( ite.type ){
case 'readFileSuccess' :
try {
builder.readJSON( currentTarget.name, ite.data );
} catch( o_O ){
vscode.window.showErrorMessage( '(T-T) ' + o_O );
return;
};
startJsonTask();
break;
case 'readFileError' :
vscode.window.showErrorMessage( '(T-T) ' + ite.error );
break;
};
};
// 4. build .html file
function build(){
try {
var currentBuild = builder.build();
} catch(o_O){
vscode.window.showErrorMessage( '(T-T) ' + o_O );
return;
};
if( currentBuild ){
vscode.window.setStatusBarMessage( '[' + progress + '/' + total + '] write [' + created + '] skiped [' + skiped + ']' );
fs.write({
path : createPath( currentTask.output, currentBuild.path ),
string : currentBuild.html,
writeIfOld : currentBuild.updatedAt
}, writeFileDispatcher );
} else {
vscode.window.setStatusBarMessage( '[' + progress + '/' + total + '] (^-^) Task complete!' );
startTask();
};
};
function writeFileDispatcher( e ){
switch( e.type ){
case 'writeFileSuccess' :
++created;
if( e.skiped ) ++skiped;
build();
break;
case 'writeFileError' :
vscode.window.showErrorMessage( '(T-T) ' + e.error );
break;
};
};
function createPath( a, b ){
if( a.charAt( a.length - 1 ) === '/' ) a = a.substr( 0, a.length - 1 );
if( b.charAt( 0 ) === '/' ) b = b.substr( 1 );
return a + '/' + b;
};
});
context.subscriptions.push( com );
};
exports.activate = activate;
// this method is called when your extension is deactivated
function deactivate() {}
exports.deactivate = deactivate;