forked from ksky521/nodeppt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodeppt.js
executable file
·137 lines (119 loc) · 3.56 KB
/
nodeppt.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
const fs = require('fs')
const path = require('path')
const exec = require('child_process').exec
const read = require('read')
require('colors')
const $ = require('./helper')
const libDir = __dirname
const rootDir = path.join(libDir, '../')
const templateDir = path.join(rootDir, 'template') + path.sep
const templateQ = [{
name: 'filename',
prompt: 'filename'.bold.green,
'default': 'demo'
}, {
name: 'title',
prompt: 'title'.bold.green,
'default': 'slide title'
}, {
name: 'subtitle',
prompt: 'subtitle'.bold.green,
'default': ''
}, {
name: 'speaker',
prompt: 'speaker'.bold.green,
'default': 'speaker'
}]
const ppt = module.exports = {
pdf: function (args) {
let url = args[0]
let output = args[1] ? args[1] : ''
if (!url) {
console.log('ERROR: pdf need a URL'.bold.red)
ppt.help('pdf')
return
}
if (output === '') {
output = 'nodeppt.pdf'
}
if (output.slice(-4) !== '.pdf') {
output += '.pdf'
}
let child = exec('phantomjs ' + libDir + '/pdf.js ' + url + ' ' + output)
// child.stderr.setEncoding('utf8')
child.stderr.on('data', function (data) {
console.log('please install phantomjs:npm install -g phantomjs'.red)
console.log('nodeppt pdf depend phantomjs '.red)
})
child.stdout.on('data', function (data) {
console.log(data)
})
},
start: function (argsObj) {
// 启动
let curRoot = process.cwd()
let dir = argsObj.dir
if (dir === '') {
dir = curRoot // path.join(rootDir, 'ppts')
}
if (!fs.existsSync(dir)) {
dir = path.join(curRoot, dir)
if (!fs.existsSync(dir)) {
return console.log('\nERROR: '.bold.red + dir + ' not a right path')
}
} else {
let stat = fs.statSync(dir)
if (!stat.isDirectory()) {
return console.log('\nERROR: '.bold.red + dir + ' not a right path')
}
}
require(libDir + '/server').start(argsObj.port, dir, argsObj.host, argsObj)
},
create: function (filename, options) {
let curRoot = process.cwd()
if (options && options.dir) {
curRoot = options.dir
}
let opts = {
isHTML: false
}
if (filename) {
filename = filename.replace(/\.htm[l]?$/, '')
opts.isHTML = /\.htm[l]?$/.test(filename)
opts.filepath = opts.isHTML ? path.join(curRoot, filename + '.html') : path.join(curRoot, filename + '.md')
if ($.exists(opts.filepath)) {
console.log('ERROR: '.bold.red + ' ' + filename + ' already exist!')
return false
}
opts.filename = filename
templateQ.splice(0, 1)
}
console.log('please input:'.bold.green);
(function next (prompt) {
if (!prompt) {
return doneTmpl(opts)
}
read(prompt, function (err, value) {
if (err) {
return console.log('\nERROR: '.bold.red + '获取 "' + prompt.name + '" 输入信息失败')
}
opts[prompt.name] = value
next(templateQ.shift())
})
})(templateQ.shift())
},
generate: function (filename, output, all, rDir) {
require(libDir + '/generate')(filename, output, all, rDir)
}
}
function doneTmpl (opts) {
if (opts.filename === '') {
opts.filename = Date.now()
}
let json = require(path.join(libDir, '../package.json'))
opts.version = json.version
opts.site = json.site
let html = $.renderFile(templateDir + (opts.isHTML ? 'default.ejs' : 'defaultmd.ejs'), opts)
$.writeFile(opts.filepath, html)
console.log(('Success:' + opts.filename + (opts.isHTML ? '.html' : '.md') + ', please write your slide content').bold.green)
}