#!/usr/bin/env node

const fs = require('fs-extra')
const path = require('path')
const CSON = require('cson')

function build_map(dest) {
  fs.writeFileSync(
    path.join(dest, 'mapspot.json'),
    JSON.stringify(CSON.load('mapspot.cson')) + '\n'
  )
  fs.writeFileSync(
    path.join(dest, 'maproute.json'),
    JSON.stringify(CSON.load('maproute.cson')) + '\n'
  )
  fs.writeFileSync(
    path.join(dest, 'maphp.json'),
    JSON.stringify(CSON.load('maphp.cson')) + '\n'
  )
  fs.writeFileSync(
    path.join(dest, 'shiptag.json'),
    JSON.stringify(CSON.load('shiptag.cson')) + '\n'
  )
}

function build_meta(dest) {
  const meta = []
  for (const fpath of fs.walkSync(dest)) {
    const fdata = JSON.parse(fs.readFileSync(fpath))
    meta.push(fdata.meta)
  }
  fs.writeFileSync(
    path.join(dest, 'meta.json'),
    JSON.stringify(meta) + '\n'
  )
}

function build() {
  const dest = '../assets/data/fcd'

  // Remove old
  fs.emptyDirSync(dest)

  // Build JSON
  build_map(dest)

  // Build meta
  build_meta(dest)
}

build()

@@ -108,34 +108,117 @@
 ')%0A%0A
-function build_map(dest) %7B
+const DEST = '../assets/data/fcd'%0A%0Afunction writeJSON(filename, data) %7B%0A  const JSON_OPTIONS = %7B spaces: '' %7D
 %0A  f
@@ -220,38 +220,33 @@
 %7D%0A  fs.write
-File
+JSON
 Sync(
-%0A    
 path.join(de
@@ -247,390 +247,311 @@
 oin(
-dest, 'mapspot.json'),%0A    JSON.stringify(CSON.load('mapspot.cson')) + '%5Cn'%0A  )%0A  fs.writeFileSync(%0A    path.join(dest, 'maproute.json'),%0A    JSON.stringify(CSON.load('maproute.cson')) + '%5Cn'%0A  )%0A  fs.writeFileSync(%0A    path.join(dest, 'maphp.json'),%0A    JSON.stringify(CSON.load('maphp.cson')) + '%5Cn'%0A  )%0A  fs.writeFileSync(%0A    path.join(dest, 'shiptag.json'),%0A    JSON.stringify(
+DEST, filename), data, JSON_OPTIONS)%0A%7D%0A%0A// If build can write in one line, put it here.%0Afunction build_misc() %7B%0A  writeJSON('mapspot.json', CSON.load('mapspot.cson'))%0A  writeJSON('maproute.json', CSON.load('maproute.cson'))%0A  writeJSON('maphp.json', CSON.load('maphp.cson'))%0A  writeJSON('shiptag.json', 
 CSON
@@ -572,27 +572,16 @@
 .cson'))
- + '%5Cn'%0A  )
 %0A%7D%0A%0Afunc
@@ -596,20 +596,16 @@
 ld_meta(
-dest
 ) %7B%0A  co
@@ -652,20 +652,20 @@
 alkSync(
-dest
+DEST
 )) %7B%0A   
@@ -750,170 +750,58 @@
 %7D%0A  
-fs.
 write
-FileSync(%0A    path.join(dest, 'meta.json'),%0A    JSON.stringify(meta) + '%5Cn'%0A  )%0A%7D%0A%0Afunction build() %7B%0A  const dest = '../assets/data/fcd'%0A%0A  // Remove old
+JSON('meta.json', meta)%0A%7D%0A%0Afunction build() %7B
 %0A  f
@@ -819,65 +819,29 @@
 ync(
-dest
+DEST
 )%0A%0A  
-// Build JSON%0A  build_map(dest)%0A%0A  // Build meta
+build_misc()
 %0A  b
@@ -854,12 +854,8 @@
 eta(
-dest
 )%0A%7D%0A

Update fcd build script