-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
25 lines (21 loc) · 719 Bytes
/
index.js
File metadata and controls
25 lines (21 loc) · 719 Bytes
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
'use strict'
const fs = require('fs-extra')
const { parse } = require('php-array-parser')
const phpArrayToJson = {
convert(file) {
const dest = file.replace(/\.[^\.]+$/, '.json')
return fs.readFile(file)
.then((source) => source.toString())
.then((contents) => extractReturningPhpArray(contents))
.then((phpArray) => parse(phpArray))
.then((obj) => fs.writeJSON(dest, obj))
}
}
function extractReturningPhpArray(contents) {
// Remove left part of return expression and any ending `?>`.
const ret = contents.indexOf('return') + 'return'.length
contents = contents.substr(ret)
contents = contents.replace(/\?>\s*$/, '_')
return contents
}
module.exports = phpArrayToJson