Skip to content

Commit 864f27d

Browse files
committed
remove q since node ^4.x; do not fail when try to download non existent file
1 parent 3df1b5d commit 864f27d

File tree

6 files changed

+75
-28
lines changed

6 files changed

+75
-28
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,5 @@ file1.js
3232
file2.js
3333
.nsmockup
3434
.nscabinet
35+
.idea
36+
test/_input

README.md

+22-12
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ _PS: This is actually also a gulp plugin._
2020

2121
```javascript
2222
var nscabinet = require('nscabinet');
23-
gulp.src('myProject/dist/**/*.js').pipe(nscabinet());
23+
gulp.src('myProject/dist/**/*.js').pipe(nscabinet({ rootPath : '/Templates' }));
2424
```
2525

26-
## Input options
26+
## Accepted input ways
2727

2828
The parameters may be stored in config files, in environment variables, or passed directly.
2929

@@ -43,6 +43,24 @@ For instance, let's say you call `nscabinet({ account : '1234' })`. Even if no e
4343

4444
For more info see [nsconfig](https://github.com/suiteplus/nsconfig).
4545

46+
## Common parameters
47+
48+
The following parameters are common through most of the methods:
49+
50+
__Connection__
51+
52+
* `realm` defaults to `netsuite.com`.
53+
54+
* `role` defaults to the account's default role.
55+
56+
* `deployment` defaults to 1.
57+
58+
__Path__
59+
60+
* `rootPath` sets the root path on the server. Defaults to `/SuiteScripts`. Must begin with `/`.
61+
62+
Example: Upload file with path `img/image.jpg` with rootPath `/Templates` will "upsert" the file
63+
onto '/Templates/img/image.jpg'.
4664

4765
## nscabinet.upload
4866

@@ -65,14 +83,6 @@ gulp.src('foo.js')
6583

6684
```
6785

68-
* `realm` defaults to `netsuite.com`.
69-
70-
* `role` defaults to the account's default role.
71-
72-
* `deployment` defaults to 1.
73-
74-
* `rootPath` defaults to `/SuiteScripts`. Must begin with `/`.
75-
7686
* `isonline` (boolean) lets you set the uploaded files to be avaliable
7787
without login.
7888

@@ -86,12 +96,12 @@ nscabinet.download(['MyProject/*.js','/Web Site Hosting Files/My Site/*.html'])
8696

8797
* `files` file selector (one or many).
8898

89-
* `*` is accepted on the file part, which is replaced by `%` on the underlying netsuite file search.
99+
* `*` is accepted on the file part. The restlet then runs a file search in which `*` is replaced with `%`.
90100

91101
* Paths are also relative to `opts.rootPath`. If a file selector begins with `/`, files will be queried
92102
by absolute path in netsuite, but saved locally inside the `cabinet_root` folder.
93103

94-
* `opts` The same options as seen in upload.
104+
* `opts` Common options.
95105

96106

97107
## nscabinet.url ( file : string , [opts] ) : Promise[string]

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
"homepage": "https://github.com/suiteplus/nscabinet#readme",
3232
"dependencies": {
3333
"nsconfig": "^0.3.0",
34-
"q": "^1.4.1",
3534
"request": "^2.64.0",
3635
"through2": "^2.0.0",
3736
"vinyl": "^1.0.0",
@@ -50,7 +49,6 @@
5049
"gulp-mocha": "^2.1.3",
5150
"gulp-plumber": "^1.0.1",
5251
"nsmockup": "^0.8.0",
53-
"q": "^1.4.1",
5452
"require-dir": "^0.3.0",
5553
"should": "^7.1.0"
5654
}

restlet/nscabinet-restlet.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var download = function (datain) {
5555
datain.files = [datain.files];
5656
}
5757

58+
var errors = [];
5859
function getFileData(file, info) {
5960
var contents = file.getValue();
6061

@@ -100,12 +101,19 @@ var download = function (datain) {
100101
outfiles = outfiles.concat(addFiles);
101102
//case 2: direct load
102103
} else {
103-
var file = nlapiLoadFile(info.pathabsolute.substr(1));
104-
outfiles = outfiles.concat([getFileData(file, info)]);
104+
try {
105+
var file = nlapiLoadFile(info.pathabsolute.substr(1));
106+
outfiles = outfiles.concat([getFileData(file, info)]);
107+
} catch (e) {
108+
errors.push(e);
109+
}
105110
}
106111
});
107112

108-
return {files: outfiles};
113+
var out = {files: outfiles};
114+
if (errors.length) out.error = errors;
115+
return out;
116+
109117
};
110118

111119

src/nscabinet.js

+22-10
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ var request = require('request'),
44
through = require('through2'),
55
vinyl = require('vinyl'),
66
nsconfig = require('nsconfig'),
7-
path = require('path'),
8-
q = require('q');
7+
path = require('path');
98

109
var PARAMS_DEF = [
1110
{name: 'rootPath', def: '/SuiteScripts'},
@@ -55,7 +54,7 @@ function upload (params) {
5554

5655

5756
module.exports.download = download;
58-
function download (files,params) {
57+
function download (files,params,info) {
5958
params = checkParams(params);
6059
var toRequest = _requestOpts(params);
6160
toRequest.json = {
@@ -72,9 +71,19 @@ function download (files,params) {
7271
function flush(cb) {
7372
var data = JSON.parse(buffer);
7473
if (data.error) {
75-
console.error(data.error.message);
76-
this.emit('error',data.error);
77-
return;
74+
data.error = data.error.map( err => {
75+
try {
76+
return JSON.parse(err)
77+
}catch(e) {
78+
//keep as it came
79+
return err;
80+
}
81+
} );
82+
data.error.forEach( e => console.error('RESTLET ERROR: ' + (e.details || e.message || e)) );
83+
info = info || {};
84+
info.errors = info.errors || [];
85+
info.errors = info.errors.concat(data.error);
86+
//this.emit('error',data.error);
7887
}
7988
data.files = data.files || [];
8089
data.files.forEach( file => {
@@ -93,6 +102,7 @@ function download (files,params) {
93102
}
94103

95104
/* STUB */
105+
/*
96106
module.exports.deleteFolder = deleteFolder;
97107
function deleteFolder (folders, params) {
98108
params = checkParams(params);
@@ -104,6 +114,7 @@ function deleteFolder (folders, params) {
104114
folders : folders
105115
};
106116
}
117+
*/
107118

108119

109120
module.exports.checkParams = checkParams;
@@ -125,11 +136,12 @@ function url(path, params) {
125136
action : 'url' ,
126137
path : params.rootPath.substr(1) + '/' + path
127138
};
128-
var deferred = q.defer();
129-
request( toRequest , (err,resp,body) => {
130-
deferred.resolve(`https://system.${params.realm}${body.url}`);
139+
return new Promise((resolve,reject) => {
140+
request( toRequest , (err,resp,body) => {
141+
if (err) return reject(err);
142+
resolve(`https://system.${params.realm}${body.url}`);
143+
});
131144
});
132-
return deferred.promise;
133145
}
134146

135147

test/nscabinet-test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,22 @@ describe('nscabinet:', function() {
165165
});
166166
*/
167167

168-
168+
169+
it('try to download 2 files, the 1st does not exist. Emit warning but still download the second', function(done){
170+
var content = randContent();
171+
fs.writeFileSync('test/_input/cnt1.txt', content);
172+
vinyl.src(['test/_input/cnt1.txt']).pipe(nscabinet()).on('finish', () => {
173+
nscabinet.download(['test/_input/cnt2.txt','test/_input/cnt1.txt'])
174+
.pipe(vinyl.dest('test/_output'))
175+
.on('finish' , () => {
176+
should(fs.existsSync('test/_output/test/_input/cnt1.txt')).be.true();
177+
should(fs.readFileSync('test/_output/test/_input/cnt1.txt').toString())
178+
.be.equal(content);
179+
done()
180+
});
181+
});
182+
183+
});
184+
185+
169186
});

0 commit comments

Comments
 (0)