shopping-checkout
Advanced tools
Comparing version 0.2.2 to 0.3.0
@@ -18,5 +18,6 @@ #!/usr/bin/env node | ||
.version( pkg.version ) | ||
.option( '-c, --cwd [cwd]' , '工作目录,默认为当前程序运行的目录' ) | ||
.option( '-f, --filename [filename]' , '模板文件夹下存放模板数据的 xlsx 文件的名字,默认为 data.xlsx' ) | ||
.option( '-p, --pictures-dir [pictures-dir]' , '一个相对于 data.xlsx 的存放图片的文件夹的相对路径,默认为 ../pictures/,' ) | ||
.option( '-c, --cwd <v>' , '工作目录,默认为当前程序运行的目录' ) | ||
.option( '-f, --filename <v>' , '模板文件夹下存放模板数据的 xlsx 文件的名字,默认为 data.xlsx' ) | ||
.option( '-p, --pictures-dir <v>' , '一个相对于 data.xlsx 的存放图片的文件夹的相对路径,默认为 ../pictures/,' ) | ||
.option( '-z, --cn' , '默认使用的谷歌翻译只在国外能用,如果要切换为国内谷歌翻译需要带上此参数' ) | ||
.parse( process.argv ); | ||
@@ -27,3 +28,4 @@ | ||
xlsxName : program.filename , | ||
pictures : program.picturesDir | ||
pictures : program.picturesDir , | ||
useCN : program.cn | ||
} ); |
@@ -6,8 +6,9 @@ 'use strict'; | ||
pathResolve = path.resolve , | ||
cons = require( 'consolidate' ) , | ||
pathDir = path.dirname , | ||
parseXLSX = require( 'xlsx' ).readFile; | ||
templatesDirPath = pathResolve( __dirname , '../templates' ); | ||
const renderer = require( './renderer' ); | ||
exports = module.exports = main; | ||
module.exports = main; | ||
@@ -20,2 +21,3 @@ /** | ||
* @param {Object} [options.picturesDir='../pictures/'] - 图片文件夹位置,相对于 data.xlsx | ||
* @param {Boolean} [options.useCN] - 默认使用国外的谷歌翻译,指定此参数则使用国内的谷歌翻译 | ||
*/ | ||
@@ -65,2 +67,6 @@ function main( options ) { | ||
/** | ||
* 每一个 Excel 文件的处理函数 | ||
* @param fileRelativePath | ||
*/ | ||
function handle( fileRelativePath ) { | ||
@@ -74,24 +80,15 @@ const templateName = pathDir( fileRelativePath ); | ||
fsp.copy( | ||
pathResolve( __dirname , './templates/' + templateName + '/resources' ) , | ||
pathResolve( workDir , './' + templateName + '/resources' ) | ||
pathResolve( templatesDirPath , templateName , './resources' ) , | ||
pathResolve( workDir , templateName , 'resources' ) | ||
); | ||
// 读取电子表格的第一张表 | ||
let wb; | ||
try { | ||
wb = parseXLSX( pathResolve( workDir , fileRelativePath ) ); | ||
} | ||
catch ( e ) { | ||
console.error( '解析 %s 文件时出错:' , pathResolve( workDir , fileRelativePath ) ); | ||
console.error( e ); | ||
console.error( '请检查文件格式是否正确。' ); | ||
} | ||
const firstSheet = wb.Sheets[ wb.SheetNames[ 0 ] ]; | ||
renderer( pathResolve( workDir , fileRelativePath ) , templateName , { | ||
useCN : options.useCN , | ||
locals : { picturesDir } , | ||
onParsed : function ( data ) { | ||
const destPath = pathResolve( workDir , templateName + '/' + data[ '分单号' ] + '.html' ); | ||
renderer( firstSheet , templateName , { | ||
locals : { picturesDir } , | ||
onRendered : function ( rowData , html ) { | ||
const destPath = pathResolve( workDir , templateName + '/' + rowData[ '分单号' ] + '.html' ); | ||
return fsp | ||
.writeFile( destPath , html ) | ||
cons | ||
.dot( pathResolve( templatesDirPath , templateName , 'template.html' ) , data ) | ||
.then( ( html )=> fsp.writeFile( destPath , html ) ) | ||
.then( ()=> { | ||
@@ -116,3 +113,3 @@ htmlCount += 1; | ||
function templates() { | ||
return fsp.readdir( pathResolve( __dirname , './templates' ) ); | ||
return fsp.readdir( templatesDirPath ); | ||
} |
@@ -11,10 +11,2 @@ 'use strict'; | ||
/** | ||
* 支持的信用卡类型 | ||
*/ | ||
const supportCardType = { | ||
'万事达' : 'master' , | ||
'visa' : 'visa' | ||
}; | ||
/** | ||
* 逐行分析表格,每当解析完一条数据后则调用一次 onData | ||
@@ -46,3 +38,3 @@ * @param sheet | ||
if ( prevRowData ) { | ||
parseData( prevRowData ); | ||
onData( prevRowData ); | ||
} | ||
@@ -57,29 +49,3 @@ | ||
} ); | ||
parseData( prevRowData ); // 最后一条分单数据 | ||
function parseData( rowData ) { | ||
// 信用卡类型 | ||
let cardTypeChinese = rowData[ '信用卡类型' ]; | ||
let cardType; | ||
if ( !cardTypeChinese ) { | ||
console.warn( '没有声明信用卡类型,默认使用 Master' ); | ||
cardTypeChinese = '万事达'; | ||
} | ||
cardTypeChinese = cardTypeChinese.toLowerCase(); | ||
cardType = supportCardType[ cardTypeChinese ]; | ||
if ( !cardType ) { | ||
console.error( '没有找到此信用卡类型:%s,默认使用万事达。' , cardTypeChinese ); | ||
cardType = 'master'; | ||
} | ||
rowData.cardType = cardType; | ||
// 计算总价 | ||
rowData[ '总价' ] = rowData.items.reduce( ( previousValue , currentItem )=> { | ||
return previousValue + Number( currentItem[ '单价' ] ) * Number( currentItem[ '数量' ] ); | ||
} , 0 ).toFixed( 2 ); | ||
onData( rowData ); | ||
} | ||
onData( prevRowData ); // 最后一条分单数据 | ||
}; |
'use strict'; | ||
const moment = require( 'moment' ); | ||
const pathResolve = require( 'path' ).resolve; | ||
const cons = require( 'consolidate' ); | ||
const parseSheet = require( './parseSheet' ); | ||
const parseSheetPerLine = require( './parseSheet' ); | ||
const transformData = require( './transform-data' ); | ||
const parseXLSX = require( 'xlsx' ).readFile; | ||
/** | ||
* @param sheet | ||
* @param {String} sheetPath - Excel 表格的绝对路径 | ||
* @param {String} templateName - 模板名称 | ||
@@ -14,39 +13,25 @@ * @param {Object} [options] | ||
*/ | ||
module.exports = ( sheet , templateName , options )=> { | ||
const templatePath = pathResolve( __dirname , 'templates/' + templateName + '/template.html' ); | ||
parseSheet( sheet , ( rowData )=> { | ||
module.exports = ( sheetPath , templateName , options )=> { | ||
// 读取电子表格的第一张表 | ||
let wb; | ||
try { | ||
wb = parseXLSX( sheetPath ); | ||
} | ||
catch ( e ) { | ||
console.error( '解析 %s 文件时出错:' , sheetPath ); | ||
console.error( e ); | ||
console.error( '请检查文件格式是否正确。' ); | ||
} | ||
const sheet = wb.Sheets[ wb.SheetNames[ 0 ] ]; | ||
switch ( templateName ) { | ||
case '6pm': | ||
[ '发货日期' , '下单日期' ].forEach( ( key )=> { | ||
const m = moment( new Date( rowData[ key ] ) ); | ||
if ( m.isValid() ) { | ||
rowData[ key ] = m.format( 'MMM D,YYYY [at] h:mm A' ); | ||
} | ||
} ); | ||
break; | ||
// 解析表格数据,每解析一行调用一次回调函数 | ||
parseSheetPerLine( sheet , ( rowData )=> { | ||
Object.assign( rowData , options.locals ); | ||
rowData.templateName = templateName; | ||
case 'amazon-us': | ||
const m1 = moment( new Date( rowData[ '下单日期' ] ) ); | ||
if ( m1.isValid() ) { | ||
rowData[ '下单日期' ] = m1.format( 'MMMM D, YYYY' ); | ||
} | ||
const m2 = moment( new Date( rowData[ '发货日期' ] ) ); | ||
if ( m2.isValid() ) { | ||
rowData[ '发货日期' ] = m2.format( 'MMM D, YYYY' ); | ||
} | ||
break; | ||
} | ||
Object.assign( rowData , options.locals ); | ||
cons.dot( templatePath , rowData ) | ||
.then( ( html )=> { | ||
options.onRendered( rowData , html ); | ||
} ); | ||
// 修改或添加表格数据 | ||
transformData( rowData , { | ||
useCN : options.useCN | ||
} ).then( options.onParsed ); | ||
} ); | ||
}; | ||
/** | ||
* 将日期转换成某一形式 | ||
*/ | ||
function covertDate( date ) {} |
{ | ||
"name": "shopping-checkout", | ||
"version": "0.2.2", | ||
"version": "0.3.0", | ||
"description": "根据数据生成购物网站的结算页面", | ||
@@ -10,7 +10,8 @@ "preferGlobal": true, | ||
"scripts": { | ||
"render": "node ./bin/cli -c ./examples" | ||
"render": "node ./bin/cli -c ./examples -z" | ||
}, | ||
"files": [ | ||
"bin", | ||
"libs" | ||
"libs", | ||
"templates" | ||
], | ||
@@ -25,2 +26,3 @@ "bin": { | ||
"dot": "^1.0.3", | ||
"translation.js": "^0.3.2", | ||
"fs-extra": "^0.26.3", | ||
@@ -27,0 +29,0 @@ "fs-promise": "^0.3.1", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
Found 1 instance in 1 package
237159
18
891
1
5
9
1
+ Addedtranslation.js@^0.3.2
+ Addedasync@1.5.2(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcomponent-emitter@1.2.1(transitive)
+ Addedcookiejar@2.0.6(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addeddebug@2.6.9(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedextend@3.0.0(transitive)
+ Addedform-data@1.0.0-rc3(transitive)
+ Addedformidable@1.0.17(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedmethods@1.1.2(transitive)
+ Addedmime@1.3.4(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedms@2.0.0(transitive)
+ Addedqs@2.3.3(transitive)
+ Addedreadable-stream@1.0.27-1(transitive)
+ Addedreduce-component@1.0.1(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedsuperagent@1.8.5(transitive)
+ Addedtranslation.js@0.3.4(transitive)