Skip to content

Commit

Permalink
#5 implement @ns.custom.concat
Browse files Browse the repository at this point in the history
  • Loading branch information
danieljoppi committed Feb 1, 2016
1 parent c3f5184 commit c1433bf
Show file tree
Hide file tree
Showing 6 changed files with 300 additions and 27 deletions.
5 changes: 3 additions & 2 deletions gulp/development.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ var gulp = require('gulp'),
paths = {
js: [
`${appRoot}/gulp/**/*.js`,
`${appRoot}/src/**/*.js`
`${appRoot}/src/**/*.js`,
`!${appRoot}/src/other/*.js`
],
jsTests: [`${appRoot}/test/**/*-test.js`]
};
Expand Down Expand Up @@ -47,7 +48,7 @@ gulp.task('dev:mocha', ['dev:eslint'], function () {
gulp.task('dev:gen-libs', function() {
return gulp.src(`${appRoot}/lib/*.js`)
.pipe(plugins.uglify())
.pipe(gulp.dest(`${appRoot}/src/other`))
.pipe(gulp.dest(`${appRoot}/src/other`));
});

gulp.task('watch', ['dev:mocha'], function () {
Expand Down
76 changes: 54 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
'use strict';
var fs = require('fs'),
path = require('path'),
nsAnnotation = require('./ns-annotation'),
browserify = require('browserify'),
source = require('vinyl-source-stream'),
buffer = require('vinyl-buffer'),
gulp = require('gulp'),
through = require('through2');


function nsify(scriptPath, opts) {
opts = opts || {};

function nsify(scriptPath) {
let script = nsAnnotation(scriptPath),
custom = script.custom || {};
custom.clientProof = custom.clientProof === 'true' || script.type === 'client';

let finalName = `${script.id}.js`,
sobj = {
alias: script.alias,
name: finalName,
config: custom || {}
},
name = sobj.name,
config = sobj.config;
config = custom || {};

if (name.indexOf('_') === 0) {
name = name.substr(1);
if (finalName.indexOf('_') === 0) {
finalName = finalName.substr(1);
}

let bns,
Expand All @@ -38,31 +30,71 @@ function nsify(scriptPath, opts) {
let bwf = browserify(scriptPath, opts_);
bwf.pipeline.get('dedupe');
bns = bwf.bundle()
.pipe(source(~name.indexOf('.js') ? name : `${name}.js`));
.pipe(source(finalName));
} else {
bns = gulp.src(scriptPath);
}

return bns.pipe(buffer())
.pipe(through.obj(function (chunk, enc, cb) {
var content = chunk.contents.toString();

content = content.replace(/['"]use strict['"];?/g, '');

if (nodeBased) {
let lastFunc = content.lastIndexOf('['),
idx = content.substr(lastFunc).replace(/[^\d]/g, '').trim();

var pre = `var script = `,
pos = `\nvar ${sobj.alias} = script(${idx});`,
out = pre + content + pos;
chunk.contents = new Buffer(out);
} else {
chunk.contents = new Buffer(content);
pos = `\nvar ${script.alias} = script(${idx});`;
content = pre + content + pos;
}

// verify - concat libs
let filesJs = [];
if (config.concat) {
let dirPath = path.dirname(scriptPath),
concat = config.concat.split(',');
for (let c = 0; c < concat.length; c++) {
let lib = concat[c];
if (!lib || !lib.trim()) {
continue;
} else {
lib = lib.replace(/['"]/g, '').trim();
let libPath = path.join(dirPath, lib);
filesJs.push(libPath);
}
}
}
this.push(chunk);
return cb();

let that = this,
chunks = [],
actual = 0,
verifyEnd = () => {
if (++actual === filesJs.length + 1) {
let finalContent = '';
for (let k = 0; k < chunks.length; k++) {
let libChunk = chunks[k],
append = libChunk.contents.toString();
finalContent += `${append}\n`;
}
finalContent += content;

chunk.contents = new Buffer(finalContent);
that.push(chunk);
return cb();
}
};

for (let f = 0; f < filesJs.length; f++) {
let libPath = filesJs[f];
nsify(libPath).pipe(through.obj(function (chunk) {
chunks.push(chunk);
verifyEnd();
}));
}
verifyEnd();
}));

}
nsify.annotation = nsAnnotation;

Expand Down
10 changes: 7 additions & 3 deletions src/ns-annotation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const
/**
* Others libraries for SuiteScript
*/
$RE_LIBS = /@ns.libs:[ ]*(['|"][\w\/'", -\.]*\n)/,
$RE_LIBS = /@ns.libs:[ ]*(['|"][\w\/'", -\.]*)\n/,
/**
* Configure any record for Client or User Event
*/
Expand Down Expand Up @@ -83,7 +83,7 @@ const
/**
* Custom Annotations
*/
$RE_CUSTOM = `@ns.custom.([A-Za-z0-9-\_]*):[ ]*["|']([A-Za-z-\_ \.\/]*)["|']`;
$RE_CUSTOM = `@ns.custom.([A-Za-z0-9-\\_]*):[ ]*["|']([\\w\\/'", -\\.]*)\\n`;

var parseStr = (l) => l.replace(/['|"]/g, '').trim();

Expand Down Expand Up @@ -112,6 +112,10 @@ var parseStr = (l) => l.replace(/['|"]/g, '').trim();
* libs: [string],
* params: object,
* [records]: [string],
* [config]: {
* [noAlias]: boolean,
* [concat]: [string]
* },
* [custom]: object
* }}
*/
Expand Down Expand Up @@ -237,7 +241,7 @@ module.exports = (scriptPath, format) => {

let param = match[1],
value = match[2];
nsObj.custom[param] = value;
nsObj.custom[param] = value.replace(/['"]/g, '').trim();
}

// Remove uncreated functions
Expand Down
59 changes: 59 additions & 0 deletions test/_files/custom/schedule-with-require.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @ns.id: 'schedule-with-require'
* @ns.desc: 'Schedule with require()'
* @ns.type: 'schedule'
*
* @ns.functions.get: 'info'
* @ns.functions.post: 'save'
* @ns.functions.put: 'update'
* @ns.functions.delete: 'remove'
*
* @ns.custom.concat: 'user-event-complex', 'suitelet-complex'
*/
'use strict';
var store = require('./libs/store');

var $trace = 'restlet-with-require';

module.exports = {
info: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.info', 'Missing Record Type: "data.type"')
} else
if (data.id) {
nlapiLogExecution('ERROR', $trace + '.info', 'Missing Internal ID: "data.id"')
} else {
return store.info(data);
}
},
save: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.save', 'Missing Record Type: "data.type"')
} else
if (data.init) {
nlapiLogExecution('ERROR', $trace + '.save', 'Missing Initial Data: "data.init"')
} else {
return store.save(data);
}
},
update: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.update', 'Missing Record Type: "data.type"')
} else
if (data.id) {
nlapiLogExecution('ERROR', $trace + '.update', 'Missing Internal ID: "data.id"')
} else {
return store.update(data);
}
},
remove: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.remove', 'Missing Record Type: "data.type"')
} else
if (data.id) {
nlapiLogExecution('ERROR', $trace + '.remove', 'Missing Internal ID: "data.id"')
} else {
return store.remove(data);
}
}
};
161 changes: 161 additions & 0 deletions test/_files/output/suite-script-nsify-schedule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
var script = function r(n,t,o){function e(f){if(!t[f]){if(!n[f])throw"Cannot find module '"+f+"'";var u=t[f]={exports:{}};n[f][0].call(u.exports,function(r){var t=n[f][1][r];return e(t?t:r)},u,u.exports,r,n,t,o)}return t[f].exports}for(var f=0;f<o.length;f++)e(o[f]);return e}({1:[function(require,module,exports){
/**
* @ns.id: 'my-user-event-complex'
* @ns.desc: 'My Userevent complex Description'
* @ns.type: 'user-event'
*
* @ns.functions.beforeLoad: 'mybeforeLoad'
* @ns.functions.beforeSubmit: 'mybeforeSubmit'
* @ns.functions.afterSubmit: 'myafterSubmit'
*
* @ns.libs: 'my-lib-01', 'my-lib-02'
*
* @ns.params.my-param_FULL: {name: 'My Param FULL', type: 'TEXT'}
* @ns.params.my-param_SiMple: 'INTEGER'
*
* @ns.record: 'my_custom_record'
*/

module.exports = {

};

},{}]},{},[1]);

var MyUserEventComplex = script(1);
var script = function r(n,t,o){function e(f){if(!t[f]){if(!n[f])throw"Cannot find module '"+f+"'";var u=t[f]={exports:{}};n[f][0].call(u.exports,function(r){var t=n[f][1][r];return e(t?t:r)},u,u.exports,r,n,t,o)}return t[f].exports}for(var f=0;f<o.length;f++)e(o[f]);return e}({1:[function(require,module,exports){
/**
* @ns.id: 'my-suitelet-complex'
* @ns.desc: 'My Suitelet complex Description'
* @ns.type: 'suitelet'
*
* @ns.function: 'myDefaultFunction'
* @ns.libs: 'my-lib-01', 'my-lib-02'
*
* @ns.params.my-param_FULL: {name: 'My Param FULL', type: 'TEXT'}
* @ns.params.my-param_SiMple: 'INTEGER'
*
* @ns.record: 'my_custom_record'
*/

module.exports = {

};

},{}]},{},[1]);

var MySuiteletComplex = script(1);
var script = function r(n,t,o){function e(f){if(!t[f]){if(!n[f])throw"Cannot find module '"+f+"'";var u=t[f]={exports:{}};n[f][0].call(u.exports,function(r){var t=n[f][1][r];return e(t?t:r)},u,u.exports,r,n,t,o)}return t[f].exports}for(var f=0;f<o.length;f++)e(o[f]);return e}({1:[function(require,module,exports){


/**
* Save Record.
*
* @param data {{type: string, init: [object]}}
*/
exports.save = function(data) {
var recType = data.type,
record = nlapiCreateRecord(recType, data.init);
nlapiSubmitRecrod(reccord);
};

/**
* Update Record.
*
* @param data {{type: string, id: string, fields: [string], values: [string]}}
*/
exports.update = function(data) {
var recType = data.type,
recId = data.id;

nlapiSubmitField(recType, recId, data.fields, data.values);
};

/**
* Remove Record.
*
* @param data {{type: string, id: string}}
*/
exports.remove = function(data) {
var recType = data.type,
recId = data.id;

nlapiDeleteRecord(recType, recId);
};

/**
* Get info Record.
*
* @param data {{type: string, id: string}}
*/
exports.info = function(data) {
var recType = data.type,
recId = data.id;

var record = nlapiLoadRecord(recType, recId);
return JSON.stringify(record);
};
},{}],2:[function(require,module,exports){
/**
* @ns.id: 'schedule-with-require'
* @ns.desc: 'Schedule with require()'
* @ns.type: 'schedule'
*
* @ns.functions.get: 'info'
* @ns.functions.post: 'save'
* @ns.functions.put: 'update'
* @ns.functions.delete: 'remove'
*
* @ns.custom.concat: 'user-event-complex', 'suitelet-complex'
*/

var store = require('./libs/store');

var $trace = 'restlet-with-require';

module.exports = {
info: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.info', 'Missing Record Type: "data.type"')
} else
if (data.id) {
nlapiLogExecution('ERROR', $trace + '.info', 'Missing Internal ID: "data.id"')
} else {
return store.info(data);
}
},
save: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.save', 'Missing Record Type: "data.type"')
} else
if (data.init) {
nlapiLogExecution('ERROR', $trace + '.save', 'Missing Initial Data: "data.init"')
} else {
return store.save(data);
}
},
update: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.update', 'Missing Record Type: "data.type"')
} else
if (data.id) {
nlapiLogExecution('ERROR', $trace + '.update', 'Missing Internal ID: "data.id"')
} else {
return store.update(data);
}
},
remove: function (data) {
if (data.type) {
nlapiLogExecution('ERROR', $trace + '.remove', 'Missing Record Type: "data.type"')
} else
if (data.id) {
nlapiLogExecution('ERROR', $trace + '.remove', 'Missing Internal ID: "data.id"')
} else {
return store.remove(data);
}
}
};

},{"./libs/store":1}]},{},[2]);

var ScheduleWithRequire = script(2);

0 comments on commit c1433bf

Please sign in to comment.