Skip to content

Commit 6bde9d8

Browse files
authored
Merge pull request #47 from ciatph/dev
v1.0.6
2 parents 05ae4d6 + 03f65d2 commit 6bde9d8

File tree

8 files changed

+75
-42
lines changed

8 files changed

+75
-42
lines changed

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
EXCEL_FILE_URL=https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/climate/tendayweatheroutlook/day1.xlsx
2+
DEFAULT_EXCEL_FILE_URL=https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/climate/tendayweatheroutlook/day1.xlsx
23
SHEETJS_COLUMN=__EMPTY
34
SORT_ALPHABETICAL=1

.github/workflows/release.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ jobs:
7979
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
8080

8181
publish-npm:
82+
name: Publish to NPM registry
8283
runs-on: ubuntu-latest
8384
needs: build-on-win
8485
steps:

README.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,22 @@ The following dependencies are used for this project. Feel free to use other dep
8888

8989
- Asks users to enter the download URL of a remote excel file or use the default local excel file
9090
- Loads and parses the local excel file in `/data/day1.xlsx` by default.
91-
- Loads and parses the downloaded excel file in `/data/datasource.xlsx` if download URL input is provided.
91+
- Loads and parses the downloaded excel file in `/data/datasource.xlsx` if download URL in the class constructor is provided.
9292
- Displays a list of available PH **region** names.
9393
- Lists all provinces and municipalities of a specified region via commandline input.
9494
- Asks for an option to write results to a JSON file.
95+
- Run the script as follows if installed using `npm i ph-municipalities`:
96+
- `node .\node_modules\ph-municipalities\src\scripts\by_region.js`
9597

9698
### `npm run list:province`
9799

98100
- Asks users to enter the download URL of a remote excel file or use the default local excel file
99101
- Loads and parses the local excel file in `/data/day1.xlsx` by default.
100-
- Loads and parses the downloaded excel file in `/data/datasource.xlsx` if download URL input is provided.
102+
- Loads and parses the downloaded excel file in `/data/datasource.xlsx` if download URL in the class constructor is provided.
101103
- Lists all municipalities under specified province(s) via commandline input.
102104
- Asks for an option to write results to a JSON file.
105+
- Run the script as follows if installed using `npm i ph-municipalities`:
106+
- `node .\node_modules\ph-municipalities\src\scripts\by_province.js`
103107

104108
### `npm run example`
105109

@@ -172,17 +176,17 @@ file = new ExcelFile({
172176

173177
// listMunicipalities() lists all municipalities
174178
// for each province
175-
const municipalitiesFromProvince =
176-
file.listMunicipalities(['Albay','Masbate','Sorsogon'])
179+
const provinces = ['Albay','Masbate','Sorsogon']
180+
const municipalitiesFromProvince = file.listMunicipalities(provinces)
177181

178-
// writeMunicipalities() writes municipalities data in a JSON file
182+
// writeMunicipalities() writes municipalities data to a JSON file
179183
file.writeMunicipalities({
180-
provinces: municipalitiesFromProvince,
184+
provinces,
181185
fileName: path.join(__dirname, 'municipalities.json'),
182186
prettify: true
183187
})
184188

185-
// JSON data of the parsed excel file will be accessible on
189+
// JSON data of the parsed excel file will is accessible on
186190
// file.datalist
187191
console.log(file.datalist)
188192
```

data/regions.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"name": "National Capital Region",
3535
"abbrev": "NCR",
3636
"region_num": "",
37-
"provinces": ["Metro Manila"]
37+
"provinces": ["Metropolitan Manila"]
3838
},
3939
{
4040
"name": "Region IV-A",

src/classes/excel/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ class ExcelFile {
253253
/**
254254
* Writes queried municipalities data to a JSON file.
255255
* Lists municipalities by by provinces.
256-
* @param {String} provinces - Array of case-sensitive province names. Starts with an upper case.
257-
* @param {String} filName - Full file path to a JSON file
256+
* @param {String[]} provinces - Array of case-sensitive province names. Starts with an upper case.
257+
* @param {String} fielName - Full file path to a JSON file
258258
* @param {Bool} prettify - Write the JSON content with proper spacings and newlines
259259
* @returns
260260
*/
@@ -263,12 +263,19 @@ class ExcelFile {
263263
throw new Error('Please enter a filename ending in .json')
264264
}
265265

266+
if (!/\.(json)$/i.test(fileName)) {
267+
throw new Error('Please enter a filename ending in .json')
268+
}
269+
266270
try {
267271
// List the municipalities
268272
const municipalities = this.listMunicipalities({ provinces })
273+
274+
const url = (this.#url) ? this.#url : `local datasource cache from ${process.env.DEFAULT_EXCEL_FILE_URL}`
275+
269276
const str = {
270277
metadata: {
271-
source: process.env.EXCEL_FILE_URL || '',
278+
source: url || '',
272279
title: 'List of PH Municipalities By Province and Region',
273280
description: 'This dataset generated with reference to the excel file contents from the source URL.',
274281
date_created: new Date().toDateString()

src/lib/selector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const selectDataSource = async () => {
1414
while (!exit) {
1515
// Prompt to enter the download URL of a remote excel file
1616
if (url === undefined) {
17-
const askDownload = await prompt('\nWould you like to download and use a remote Excel file?\nPress enter to ignore. Press Y and enter to proceed. [n/Y]:')
17+
const askDownload = await prompt('\nWould you like to download and use a remote Excel file?\nPress enter to ignore. Press Y and enter to proceed. [n/Y]: ')
1818

1919
if (askDownload === 'Y') {
2020
const pathToFile = path.join(process.cwd(), 'datasource.xlsx')

src/scripts/by_province.js

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ const selectDataSource = require('../lib/selector')
77
// Lists all municipalities under the specified provinces.
88
const main = async () => {
99
let exit = false
10-
let ExcelHandler
10+
let ExcelHandler = null
1111

1212
while (!exit) {
1313
// Prompt to enter the download URL of a remote excel file or use the default local excel file
14-
ExcelHandler = await selectDataSource()
14+
if (ExcelHandler === null) {
15+
ExcelHandler = await selectDataSource()
16+
}
1517

1618
if (ExcelHandler !== null) {
1719
// Prompt to ask for province name(s)
@@ -20,29 +22,38 @@ const main = async () => {
2022
if (provinces) {
2123
// List the municipalities of a targets province(s)
2224
const { total, data } = formatDisplay(ExcelHandler.listMunicipalities({ provinces }))
23-
console.log(data)
24-
console.log(`\nTotal: ${total}`)
25-
}
2625

27-
// Prompt to write results to a JSON file
28-
const write = await prompt('\nWrite results to a JSON file? [n/Y]: ')
26+
if (total === 0) {
27+
await prompt('No municipalities to show.\n')
28+
} else {
29+
console.log(data)
30+
console.log(`\nTotal: ${total}`)
2931

30-
if (write === 'Y') {
31-
const fileName = await prompt('\nEnter the JSON filename: ')
32+
// Prompt to write results to a JSON file
33+
const write = await prompt('\nWrite results to a JSON file?\nPress enter to ignore. Press Y and enter to proceed. [n/Y]: ')
3234

33-
// Use process.cwd() to enable file paths when building with "pkg"
34-
const filePath = path.join(process.cwd(), fileName)
35+
if (write === 'Y') {
36+
const fileName = await prompt('\nEnter the JSON filename: ')
3537

36-
ExcelHandler.writeMunicipalities({
37-
provinces,
38-
fileName: filePath
39-
})
38+
// Use process.cwd() to enable file paths when building with "pkg"
39+
const filePath = path.join(process.cwd(), fileName)
4040

41-
console.log(`JSON file created in ${filePath}`)
42-
}
41+
try {
42+
ExcelHandler.writeMunicipalities({
43+
provinces,
44+
fileName: filePath
45+
})
4346

44-
const ex = await prompt('\nExit? (Enter X to exit): ')
45-
exit = (ex === 'X')
47+
console.log(`JSON file created in ${filePath}\n`)
48+
} catch (err) {
49+
console.log(err.message)
50+
}
51+
52+
const ex = await prompt('Exit? (Enter X to exit): ')
53+
exit = (ex === 'X')
54+
}
55+
}
56+
}
4657
}
4758
}
4859

src/scripts/by_region.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ const regions = require('../../data/regions.json')
88
// Lists all municipalities under the provinces of a region.
99
const main = async () => {
1010
let exit = false
11-
let ExcelHandler
11+
let ExcelHandler = null
12+
let idx
1213

1314
while (!exit) {
1415
// Prompt to enter the download URL of a remote excel file or use the default local excel file
15-
ExcelHandler = await selectDataSource()
16+
if (ExcelHandler === null) {
17+
ExcelHandler = await selectDataSource()
18+
}
1619

1720
if (ExcelHandler !== null) {
1821
// Display region abbreviations
@@ -26,10 +29,10 @@ const main = async () => {
2629

2730
if (region) {
2831
// Check if the region name exists in the masterlist
29-
const idx = regions.data.findIndex(item => item.name === region)
32+
idx = regions.data.findIndex(item => item.name === region)
3033

3134
if (idx === -1) {
32-
console.log('Region name not found.')
35+
await prompt('Region name not found.\n')
3336
} else {
3437
// List the provinces of a target region
3538
const provinces = regions.data.find(x => x.name === region).provinces
@@ -48,18 +51,24 @@ const main = async () => {
4851
// Use process.cwd() to enable file paths when building with "pkg"
4952
const filePath = path.join(process.cwd(), fileName)
5053

51-
ExcelHandler.writeMunicipalities({
52-
provinces,
53-
fileName: filePath
54-
})
54+
try {
55+
ExcelHandler.writeMunicipalities({
56+
provinces,
57+
fileName: filePath
58+
})
5559

56-
console.log(`JSON file created in ${filePath}`)
60+
console.log(`JSON file created in ${filePath}`)
61+
} catch (err) {
62+
console.log(err.message)
63+
}
5764
}
5865
}
5966
}
6067

61-
const ex = await prompt('\nExit? (Enter X to exit): ')
62-
exit = (ex === 'X')
68+
if (idx >= 0) {
69+
const ex = await prompt('\nExit? (Enter X to exit): ')
70+
exit = (ex === 'X')
71+
}
6372
}
6473
}
6574

0 commit comments

Comments
 (0)