@unvt/charites 0.1.1 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/.all-contributorsrc +24 -0
  2. package/.eslintignore +2 -0
  3. package/.eslintrc.js +17 -0
  4. package/.github/CONTRIBUTING.md +30 -0
  5. package/.github/ISSUE_TEMPLATE/bug_report.md +38 -0
  6. package/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  7. package/.github/PULL_REQUEST_TEMPLATE.md +25 -0
  8. package/.github/workflows/build-docs.yml +72 -0
  9. package/.github/workflows/build.yml +1 -0
  10. package/.prettierrc.js +6 -0
  11. package/LICENSE +1 -1
  12. package/README.md +23 -106
  13. package/dist/cli/build.js +63 -0
  14. package/dist/cli/convert.js +19 -0
  15. package/dist/cli/init.js +26 -0
  16. package/dist/cli/serve.js +34 -0
  17. package/dist/cli.js +10 -66
  18. package/dist/commands/build.js +39 -3
  19. package/dist/commands/convert.js +6 -23
  20. package/dist/commands/init.js +15 -22
  21. package/dist/commands/serve.js +8 -3
  22. package/dist/lib/build-sprite.js +71 -0
  23. package/dist/lib/defaultValues.js +3 -3
  24. package/dist/lib/error.js +9 -0
  25. package/dist/lib/get-sprite-slug.js +16 -0
  26. package/dist/lib/tileinfo-importer/base-importer.js +41 -0
  27. package/dist/lib/tileinfo-importer/index.js +6 -0
  28. package/dist/lib/tileinfo-importer/metadata-importer.js +38 -0
  29. package/dist/lib/tileinfo-importer/tilejson-importer.js +25 -0
  30. package/dist/lib/validate-style.js +2 -2
  31. package/dist/lib/yaml-parser.js +2 -2
  32. package/dist/lib/yaml-writer.js +48 -0
  33. package/dist/types/index.js +15 -0
  34. package/dist/types/metadatajson.js +2 -0
  35. package/dist/types/tilejson.js +2 -0
  36. package/dist/types/vector_layers.js +2 -0
  37. package/docs/.tx/config +62 -0
  38. package/docs/Makefile +170 -0
  39. package/docs/Pipfile +20 -0
  40. package/docs/Pipfile.lock +429 -0
  41. package/docs/README.md +43 -0
  42. package/docs/make.bat +35 -0
  43. package/docs/source/_static/.gitkeep +0 -0
  44. package/docs/source/_templates/.gitkeep +0 -0
  45. package/docs/source/conf.py +69 -0
  46. package/docs/source/development/index.rst +40 -0
  47. package/docs/source/index.rst +61 -0
  48. package/docs/source/install/index.rst +10 -0
  49. package/docs/source/install/install.rst +7 -0
  50. package/docs/source/install/install_on_nanban.rst +9 -0
  51. package/docs/source/install/recommended_environment.rst +6 -0
  52. package/docs/source/usage/commandline_interface.rst +102 -0
  53. package/docs/source/usage/examples.rst +74 -0
  54. package/docs/source/usage/global_options.rst +21 -0
  55. package/docs/source/usage/index.rst +10 -0
  56. package/package.json +12 -4
  57. package/provider/default/app.css +10 -0
  58. package/provider/default/app.js +31 -5
  59. package/provider/default/index.html +7 -0
  60. package/provider/geolonia/app.css +10 -0
  61. package/provider/geolonia/app.js +29 -5
  62. package/provider/geolonia/index.html +7 -0
  63. package/provider/mapbox/app.css +10 -0
  64. package/provider/mapbox/app.js +31 -5
  65. package/provider/mapbox/index.html +7 -0
  66. package/src/cli/build.ts +77 -0
  67. package/src/cli/convert.ts +18 -0
  68. package/src/cli/init.ts +34 -0
  69. package/src/cli/serve.ts +39 -0
  70. package/src/cli.ts +12 -76
  71. package/src/commands/build.ts +71 -9
  72. package/src/commands/convert.ts +16 -35
  73. package/src/commands/init.ts +28 -21
  74. package/src/commands/serve.ts +70 -57
  75. package/src/lib/build-sprite.ts +80 -0
  76. package/src/lib/defaultValues.ts +6 -6
  77. package/src/lib/error.ts +6 -0
  78. package/src/lib/get-sprite-slug.ts +18 -0
  79. package/src/lib/tileinfo-importer/base-importer.ts +57 -0
  80. package/src/lib/tileinfo-importer/index.ts +2 -0
  81. package/src/lib/tileinfo-importer/metadata-importer.ts +44 -0
  82. package/src/lib/tileinfo-importer/tilejson-importer.ts +29 -0
  83. package/src/lib/validate-style.ts +8 -2
  84. package/src/lib/yaml-parser.ts +9 -8
  85. package/src/lib/yaml-writer.ts +68 -0
  86. package/src/types/index.ts +3 -0
  87. package/src/types/metadatajson.ts +16 -0
  88. package/src/types/tilejson.ts +25 -0
  89. package/src/types/vector_layers.ts +11 -0
  90. package/test/build-sprite.spec.ts +24 -0
  91. package/test/build.spec.ts +121 -16
  92. package/test/convert.spec.ts +7 -7
  93. package/test/data/icons/aerialway.svg +4 -0
  94. package/test/data/init/init.yml +6 -0
  95. package/test/data/init/init_metadata.yml +60 -0
  96. package/test/data/init/init_tilejson.yml +28 -0
  97. package/test/data/init/init_tilejson_without_layers.yml +9 -0
  98. package/test/data/init/tilejson/init_decomposite.yml +13 -0
  99. package/test/data/init/tilejson/layers/bicycle_parking.yml +6 -0
  100. package/test/data/init/tilejson/layers/showers.yml +6 -0
  101. package/test/data/init/tilejson/layers/telephone.yml +6 -0
  102. package/test/get-sprite-slug.spec.ts +34 -0
  103. package/test/init.spec.ts +151 -0
  104. package/test/validate-style.spec.ts +7 -5
  105. package/test/yaml-parser.spec.ts +15 -15
  106. package/tsconfig.json +3 -1
@@ -3,11 +3,37 @@ mapboxgl.accessToken = '___MAPBOX_ACCESS_TOKEN___'
3
3
  const map = new mapboxgl.Map({
4
4
  container: 'map',
5
5
  hash: true,
6
- style: `http://${window.location.host}/style.json`
7
- });
6
+ style: `http://${window.location.host}/style.json`,
7
+ })
8
8
 
9
- const socket = new WebSocket('ws://localhost:___PORT___');
9
+ const socket = new WebSocket('ws://localhost:___PORT___')
10
10
 
11
- socket.addEventListener('message',(message)=>{
11
+ socket.addEventListener('message', (message) => {
12
12
  map.setStyle(JSON.parse(message.data))
13
- });
13
+ })
14
+
15
+ map.addControl(new mapboxgl.NavigationControl(), 'top-right')
16
+
17
+ const showTileBoundaries = document.getElementById('showTileBoundaries')
18
+ const setShowTileBoundaries = function () {
19
+ const checked = showTileBoundaries.checked
20
+ map.showTileBoundaries = checked
21
+ }
22
+ setShowTileBoundaries()
23
+ showTileBoundaries.addEventListener('click', setShowTileBoundaries)
24
+
25
+ const showCollisionBoxes = document.getElementById('showCollisionBoxes')
26
+ const setShowCollisionBoxes = function () {
27
+ const checked = showCollisionBoxes.checked
28
+ map.showCollisionBoxes = checked
29
+ }
30
+ setShowCollisionBoxes()
31
+ showCollisionBoxes.addEventListener('click', setShowCollisionBoxes)
32
+
33
+ const showPadding = document.getElementById('showPadding')
34
+ const setShowPadding = function () {
35
+ const checked = showPadding.checked
36
+ map.showPadding = checked
37
+ }
38
+ setShowPadding()
39
+ showPadding.addEventListener('click', setShowPadding)
@@ -7,6 +7,13 @@
7
7
  <script src='https://api.mapbox.com/mapbox-gl-js/v2.5.0/mapbox-gl.js'></script>
8
8
  </head>
9
9
  <body>
10
+ <div class="overlay">
11
+ <label><input type="checkbox" id="showTileBoundaries">show Tile Boundaries</label>
12
+ <br>
13
+ <label><input type="checkbox" id="showCollisionBoxes">show Collision Boxes</label>
14
+ <br>
15
+ <label><input type="checkbox" id="showPadding">show Padding</label>
16
+ </div>
10
17
  <div id="map"></div>
11
18
  <script type="text/javascript" src="/app.js"></script>
12
19
  </body>
@@ -0,0 +1,77 @@
1
+ import { Command } from 'commander'
2
+ import { build, buildOptions, buildWatch } from '../commands/build'
3
+ import { error } from '../lib/error'
4
+ import { defaultSettings } from '../lib/defaultValues'
5
+ import fs from 'fs'
6
+ import path from 'path'
7
+
8
+ const program = new Command()
9
+ program
10
+ .name('build')
11
+ .arguments('<source> [destination]')
12
+ .description('build a style JSON from the YAML')
13
+ .option('-c, --compact-output', 'build a minified style JSON')
14
+ .option('-w, --watch', 'watch YAML and build when changed')
15
+ .option(
16
+ '-u, --sprite-url [<sprite url>]',
17
+ 'url to set as the sprite in style.json',
18
+ )
19
+ .option(
20
+ '-i, --sprite-input [<icon input directory>]',
21
+ 'directory path of icon source to build icons. The default <icon source> is `icons/`',
22
+ )
23
+ .option(
24
+ '-o, --sprite-output [<icon output directory>]',
25
+ 'directory path to output icon files. The default <icons destination> is the current directory',
26
+ )
27
+ .option(
28
+ '--provider [provider]',
29
+ 'your map service. e.g. `mapbox`, `geolonia`',
30
+ )
31
+ .action(
32
+ async (source: string, destination: string, buildOptions: buildOptions) => {
33
+ const options = program.opts()
34
+ options.provider = buildOptions.provider
35
+ options.compactOutput = buildOptions.compactOutput
36
+ options.spriteUrl = buildOptions.spriteUrl
37
+ options.spriteOutput = buildOptions.spriteOutput || process.cwd()
38
+
39
+ const spriteInputDefault = path.resolve(process.cwd(), 'icons')
40
+
41
+ if (buildOptions.spriteInput) {
42
+ options.spriteInput = buildOptions.spriteInput
43
+ } else if (fs.existsSync(spriteInputDefault)) {
44
+ options.spriteInput = spriteInputDefault
45
+ }
46
+
47
+ if (!fs.existsSync(defaultSettings.configFile)) {
48
+ fs.writeFileSync(
49
+ defaultSettings.configFile,
50
+ `provider: ${options.provider || 'default'}`,
51
+ )
52
+ }
53
+
54
+ if (buildOptions.watch) {
55
+ try {
56
+ console.log('Start watching...')
57
+ await new Promise((resolve) => {
58
+ const watcher = buildWatch(source, destination, options)
59
+ process.on('SIGINT', () => {
60
+ watcher.close()
61
+ resolve(undefined)
62
+ })
63
+ })
64
+ } catch (e) {
65
+ error(e)
66
+ }
67
+ } else {
68
+ try {
69
+ await build(source, destination, options)
70
+ } catch (e) {
71
+ error(e)
72
+ }
73
+ }
74
+ },
75
+ )
76
+
77
+ export default program
@@ -0,0 +1,18 @@
1
+ import { Command } from 'commander'
2
+ import { convert } from '../commands/convert'
3
+ import { error } from '../lib/error'
4
+
5
+ const program = new Command()
6
+ program
7
+ .name('convert')
8
+ .arguments('<source> [destination]')
9
+ .description('convert the style JSON to YAML')
10
+ .action((source: string, destination: string) => {
11
+ try {
12
+ convert(source, destination)
13
+ } catch (e) {
14
+ error(e)
15
+ }
16
+ })
17
+
18
+ export default program
@@ -0,0 +1,34 @@
1
+ import { Command } from 'commander'
2
+ import { init, initOptions } from '../commands/init'
3
+ import { error } from '../lib/error'
4
+
5
+ const program = new Command()
6
+ program
7
+ .name('init')
8
+ .arguments('<file>')
9
+ .description('initialize a style JSON')
10
+ .option(
11
+ '-t, --tilejson-urls <tilejson_urls>',
12
+ 'an URL for TileJSON. It will create empty layers from vector_layers property of TileJSON. Please use comma (,) in case multiple TileJSONs require.',
13
+ )
14
+ .option(
15
+ '-m, --metadatajson-urls <metadatajson_urls>',
16
+ 'an URL for metadata.json. It will create empty layers from vector_layers property of metadata.json. Please use comma (,) in case multiple metadata.json require.',
17
+ )
18
+ .option(
19
+ '-c, --composite-layers',
20
+ 'If it is true, a single YAML will be generated with multiple layers. Default is false.',
21
+ )
22
+ .action(async (file: string, initOptions: initOptions) => {
23
+ const options = program.opts()
24
+ options.tilejsonUrls = initOptions.tilejsonUrls
25
+ options.metadatajsonUrls = initOptions.metadatajsonUrls
26
+ options.compositeLayers = initOptions.compositeLayers
27
+ try {
28
+ await init(file, options)
29
+ } catch (e) {
30
+ error(e)
31
+ }
32
+ })
33
+
34
+ export default program
@@ -0,0 +1,39 @@
1
+ import { Command } from 'commander'
2
+ import { serve, serveOptions } from '../commands/serve'
3
+ import { error } from '../lib/error'
4
+ import { defaultSettings } from '../lib/defaultValues'
5
+ import fs from 'fs'
6
+
7
+ const program = new Command()
8
+ program
9
+ .name('serve')
10
+ .arguments('<source>')
11
+ .description('serve your map locally')
12
+ .option(
13
+ '--provider [provider]',
14
+ 'your map service. e.g. `mapbox`, `geolonia`',
15
+ )
16
+ .option(
17
+ '--mapbox-access-token [mapboxAccessToken]',
18
+ 'Access Token for the Mapbox',
19
+ )
20
+ .option('--port [port]', 'Specify custom port')
21
+ .action((source: string, serveOptions: serveOptions) => {
22
+ const options: serveOptions = program.opts()
23
+ options.provider = serveOptions.provider
24
+ options.mapboxAccessToken = serveOptions.mapboxAccessToken
25
+ options.port = serveOptions.port
26
+ if (!fs.existsSync(defaultSettings.configFile)) {
27
+ fs.writeFileSync(
28
+ defaultSettings.configFile,
29
+ `provider: ${options.provider || 'default'}`,
30
+ )
31
+ }
32
+ try {
33
+ serve(source, program.opts())
34
+ } catch (e) {
35
+ error(e)
36
+ }
37
+ })
38
+
39
+ export default program
package/src/cli.ts CHANGED
@@ -1,83 +1,19 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { Command } from 'commander';
4
- import fs from 'fs'
3
+ import { Command } from 'commander'
5
4
 
6
- import { init } from './commands/init'
7
- import { convert } from './commands/convert'
8
- import { build } from './commands/build'
9
- import { serve } from './commands/serve'
10
-
11
- import { defaultSettings } from './lib/defaultValues'
12
-
13
- interface buildOptions {
14
- compactOutput?: boolean
15
- }
16
-
17
- const program = new Command();
18
-
19
- const error = (message: any) => {
20
- console.error(message.toString())
21
- process.exit(1)
22
- }
23
-
24
- program
25
- .option('--provider [provider]', 'your map service. e.g. `mapbox`, `geolonia`')
26
- .option('--mapbox-access-token [mapboxAccessToken]', 'Access Token for the Mapbox')
27
-
28
- program
29
- .command('init <file>')
30
- .description('initialize a style JSON')
31
- .action((file: string) => {
32
- try {
33
- init(file)
34
- } catch(e) {
35
- error(e)
36
- }
37
- })
38
-
39
- program
40
- .command('convert <source> [destination]')
41
- .description('convert the style JSON to YAML')
42
- .action((source: string, destination: string) => {
43
- try {
44
- convert(source, destination)
45
- } catch(e) {
46
- error(e)
47
- }
48
- })
49
-
50
- program
51
- .command('build <source> [destination]')
52
- .description('build a style JSON from the YAML')
53
- .option('-c, --compact-output', 'build a minified style JSON')
54
- .action((source: string, destination: string, buildOptions: buildOptions) => {
55
- const options = program.opts()
56
- options.compactOutput = buildOptions.compactOutput
57
-
58
- if (! fs.existsSync(defaultSettings.configFile)) {
59
- fs.writeFileSync(defaultSettings.configFile, `provider: ${options.provider || 'default'}`)
60
- }
61
- try {
62
- build(source, destination, options)
63
- } catch(e) {
64
- error(e)
65
- }
66
- })
5
+ import init from './cli/init'
6
+ import convert from './cli/convert'
7
+ import build from './cli/build'
8
+ import serve from './cli/serve'
67
9
 
10
+ const program = new Command()
11
+ const version = require('../package.json').version
68
12
  program
69
- .command('serve <source>')
70
- .description('serve your map locally')
71
- .action((source: string) => {
72
- const options = program.opts()
73
- if (! fs.existsSync(defaultSettings.configFile)) {
74
- fs.writeFileSync(defaultSettings.configFile, `provider: ${options.provider || 'default'}`)
75
- }
76
- try {
77
- serve(source, program.opts())
78
- } catch(e) {
79
- error(e)
80
- }
81
- })
13
+ .version(version, '-v, --version', 'output the version number')
14
+ .addCommand(init)
15
+ .addCommand(convert)
16
+ .addCommand(build)
17
+ .addCommand(serve)
82
18
 
83
19
  program.parse(process.argv)
@@ -2,15 +2,27 @@ import path from 'path'
2
2
  import fs from 'fs'
3
3
  import { parser } from '../lib/yaml-parser'
4
4
  import { validateStyle } from '../lib/validate-style'
5
+ import { buildSprite } from '../lib/build-sprite'
6
+ import { getSpriteSlug } from '../lib/get-sprite-slug'
5
7
  import { defaultValues } from '../lib/defaultValues'
6
8
  import jsonminify from 'jsonminify'
9
+ import { StyleSpecification } from '@maplibre/maplibre-gl-style-spec/types'
10
+ import watch from 'node-watch'
7
11
 
8
- interface options {
9
- provider?: string,
12
+ export interface buildOptions {
13
+ provider?: string
10
14
  compactOutput?: boolean
15
+ watch?: boolean
16
+ spriteUrl?: string
17
+ spriteInput?: string
18
+ spriteOutput?: string
11
19
  }
12
20
 
13
- export function build(source: string, destination: string, options: options) {
21
+ export async function build(
22
+ source: string,
23
+ destination: string,
24
+ options: buildOptions,
25
+ ) {
14
26
  let sourcePath = path.resolve(process.cwd(), source)
15
27
 
16
28
  // The `source` is absolute path.
@@ -18,11 +30,11 @@ export function build(source: string, destination: string, options: options) {
18
30
  sourcePath = source
19
31
  }
20
32
 
21
- if (! fs.existsSync(sourcePath)) {
33
+ if (!fs.existsSync(sourcePath)) {
22
34
  throw `${sourcePath}: No such file or directory`
23
35
  }
24
36
 
25
- let destinationPath = ""
37
+ let destinationPath = ''
26
38
 
27
39
  if (destination) {
28
40
  if (destination.match(/^\//)) {
@@ -31,7 +43,10 @@ export function build(source: string, destination: string, options: options) {
31
43
  destinationPath = path.resolve(process.cwd(), destination)
32
44
  }
33
45
  } else {
34
- destinationPath = path.join(path.dirname(sourcePath), `${path.basename(source, '.yml')}.json`)
46
+ destinationPath = path.join(
47
+ path.dirname(sourcePath),
48
+ `${path.basename(source, '.yml')}.json`,
49
+ )
35
50
  }
36
51
 
37
52
  let provider = defaultValues.provider
@@ -42,13 +57,36 @@ export function build(source: string, destination: string, options: options) {
42
57
  let style = ''
43
58
 
44
59
  try {
45
- const _style = parser(sourcePath)
60
+ const _style: StyleSpecification = parser(sourcePath)
46
61
  validateStyle(_style, provider)
62
+
63
+ if (options.spriteUrl && 'sprite' in _style) {
64
+ _style.sprite = options.spriteUrl
65
+ }
66
+
47
67
  style = JSON.stringify(_style, null, ' ')
68
+
69
+ if (options.spriteInput && options.spriteOutput) {
70
+ if (!fs.existsSync(options.spriteInput)) {
71
+ throw `${options.spriteInput}: No such directory. Please specify valid icon input directory. For more help run charites build --help`
72
+ }
73
+
74
+ if (!fs.existsSync(options.spriteOutput)) {
75
+ throw `${options.spriteOutput}: No such directory. Please specify valid icon output directory. For more help run charites build --help`
76
+ }
77
+
78
+ const iconSlug = getSpriteSlug(JSON.parse(style))
79
+ if (!iconSlug) {
80
+ throw `Invalid sprite url format.`
81
+ }
82
+
83
+ await buildSprite(options.spriteInput, options.spriteOutput, iconSlug)
84
+ }
85
+
48
86
  if (options.compactOutput) {
49
87
  style = jsonminify(style)
50
88
  }
51
- } catch(err) {
89
+ } catch (err) {
52
90
  if (err) {
53
91
  throw err
54
92
  } else {
@@ -58,7 +96,31 @@ export function build(source: string, destination: string, options: options) {
58
96
 
59
97
  try {
60
98
  fs.writeFileSync(destinationPath, style)
61
- } catch(err) {
99
+ } catch (err) {
62
100
  throw `${destinationPath}: Permission denied`
63
101
  }
64
102
  }
103
+
104
+ export function buildWatch(
105
+ source: string,
106
+ destination: string,
107
+ options: buildOptions,
108
+ ) {
109
+ let sourcePath = path.resolve(process.cwd(), source)
110
+ if (source.match(/^\//)) {
111
+ sourcePath = source
112
+ }
113
+ console.log(path.dirname(sourcePath))
114
+ return watch(
115
+ path.dirname(sourcePath),
116
+ { recursive: true, filter: /\.yml$/ },
117
+ (event, file) => {
118
+ console.log(`${(event || '').toUpperCase()}: ${file}`)
119
+ try {
120
+ build(source, destination, options)
121
+ } catch (e) {
122
+ // Nothing to do
123
+ }
124
+ },
125
+ )
126
+ }
@@ -1,31 +1,9 @@
1
1
  import path from 'path'
2
2
  import fs from 'fs'
3
- import YAML from 'js-yaml'
4
3
  import readline from 'readline'
4
+ import { writeYaml } from '../lib/yaml-writer'
5
5
 
6
- // TODO: Type of style should be loaded from maplibre or mapbox style spec.
7
- const writeYaml = (destinationPath: string, style: any) => {
8
- const layers = []
9
-
10
- for (let i = 0; i < style.layers.length; i++) {
11
- const layer = style.layers[i]
12
- const layerYml = YAML.dump(layer)
13
- const fileName = `${style.layers[i].id}.yml`
14
- const dirName = path.join(path.dirname(destinationPath), 'layers')
15
- fs.mkdirSync(dirName, { recursive: true })
16
- fs.writeFileSync(path.join(dirName, fileName), layerYml)
17
-
18
- layers.push(`!!inc/file ${path.join('layers', fileName)}`)
19
- }
20
-
21
- style.layers = layers
22
-
23
- fs.writeFileSync(destinationPath, YAML.dump(style).replace(/'\!\!inc\/file layers\/.+\.yml'/g, function (match) {
24
- return match.replace(/'/g, '')
25
- }))
26
- }
27
-
28
- const getDestinationPath = (destination: string, sourcePath: string = '') => {
6
+ const getDestinationPath = (destination: string, sourcePath = '') => {
29
7
  let destinationPath
30
8
 
31
9
  if (destination) {
@@ -36,7 +14,10 @@ const getDestinationPath = (destination: string, sourcePath: string = '') => {
36
14
  }
37
15
  } else {
38
16
  if (sourcePath) {
39
- destinationPath = path.join(path.dirname(sourcePath), `${path.basename(sourcePath, '.json')}.yml`)
17
+ destinationPath = path.join(
18
+ path.dirname(sourcePath),
19
+ `${path.basename(sourcePath, '.json')}.yml`,
20
+ )
40
21
  } else {
41
22
  destinationPath = path.join(process.cwd(), 'style.yml')
42
23
  }
@@ -51,22 +32,22 @@ export function convert(source: string, destination: string) {
51
32
  if ('-' === source) {
52
33
  const rl = readline.createInterface({
53
34
  input: process.stdin,
54
- terminal: false
55
- });
35
+ terminal: false,
36
+ })
56
37
 
57
38
  const lines: string[] = []
58
39
 
59
- rl.on("line", (line) => {
40
+ rl.on('line', (line) => {
60
41
  lines.push(line)
61
- });
42
+ })
62
43
 
63
- rl.on("close", () => {
44
+ rl.on('close', () => {
64
45
  const style = JSON.parse(lines.join(''))
65
46
  const destinationPath = getDestinationPath(destination)
66
47
 
67
48
  try {
68
- writeYaml(destinationPath, style)
69
- } catch(err) {
49
+ writeYaml(destinationPath, style, false)
50
+ } catch (err) {
70
51
  throw `${destinationPath}: Permission denied`
71
52
  }
72
53
  })
@@ -78,7 +59,7 @@ export function convert(source: string, destination: string) {
78
59
  sourcePath = source
79
60
  }
80
61
 
81
- if (! fs.existsSync(sourcePath)) {
62
+ if (!fs.existsSync(sourcePath)) {
82
63
  throw `${sourcePath}: No such file or directory`
83
64
  }
84
65
 
@@ -87,8 +68,8 @@ export function convert(source: string, destination: string) {
87
68
  const destinationPath = getDestinationPath(destination, sourcePath)
88
69
 
89
70
  try {
90
- writeYaml(destinationPath, style)
91
- } catch(err) {
71
+ writeYaml(destinationPath, style, false)
72
+ } catch (err) {
92
73
  throw `${destinationPath}: Permission denied`
93
74
  }
94
75
  }
@@ -1,29 +1,36 @@
1
- import path from 'path'
2
- import fs from 'fs'
3
- import YAML from 'js-yaml'
1
+ import { StyleSpecification } from '@maplibre/maplibre-gl-style-spec/types'
2
+ import { writeYaml } from '../lib/yaml-writer'
3
+ import {
4
+ TileJSONImporter,
5
+ MetadataJSONImporter,
6
+ } from '../lib/tileinfo-importer'
4
7
 
5
- // TODO: We need type definition for style.
6
- const styleRoot = {
8
+ export interface initOptions {
9
+ tilejsonUrls?: string
10
+ metadatajsonUrls?: string
11
+ compositeLayers?: boolean
12
+ }
13
+
14
+ const styleRoot: StyleSpecification = {
7
15
  version: 8,
8
- name: "My Style",
9
- sprite: "",
10
- glyphs: "",
16
+ name: 'My Style',
17
+ sprite: '',
18
+ glyphs: '',
11
19
  sources: {},
12
- layers: []
20
+ layers: [],
13
21
  }
14
22
 
15
- export function init(file: string) {
16
- const styleYAML = YAML.dump(styleRoot)
17
- let stylePath = path.resolve(process.cwd(), file)
18
-
19
- // The `source` is absolute path.
20
- if (file.match(/^\//)) {
21
- stylePath = file
23
+ export async function init(file: string, options: initOptions) {
24
+ let styleTemplate = JSON.parse(JSON.stringify(styleRoot))
25
+ if (options.tilejsonUrls) {
26
+ const tileJSONImporter = new TileJSONImporter(options.tilejsonUrls)
27
+ styleTemplate = await tileJSONImporter.import(styleTemplate)
22
28
  }
23
-
24
- try {
25
- fs.writeFileSync(stylePath, styleYAML)
26
- } catch(err) {
27
- throw `${stylePath}: Permission denied`
29
+ if (options.metadatajsonUrls) {
30
+ const metadataJSONImporter = new MetadataJSONImporter(
31
+ options.metadatajsonUrls,
32
+ )
33
+ styleTemplate = await metadataJSONImporter.import(styleTemplate)
28
34
  }
35
+ writeYaml(file, styleTemplate, options.compositeLayers)
29
36
  }