Skip to content

Commit

Permalink
Merge pull request #47 from ciatph/dev
Browse files Browse the repository at this point in the history
v1.0.6
  • Loading branch information
ciatph authored Jan 29, 2023
2 parents 05ae4d6 + 03f65d2 commit 6bde9d8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 42 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
EXCEL_FILE_URL=https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/climate/tendayweatheroutlook/day1.xlsx
DEFAULT_EXCEL_FILE_URL=https://pubfiles.pagasa.dost.gov.ph/pagasaweb/files/climate/tendayweatheroutlook/day1.xlsx
SHEETJS_COLUMN=__EMPTY
SORT_ALPHABETICAL=1
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

publish-npm:
name: Publish to NPM registry
runs-on: ubuntu-latest
needs: build-on-win
steps:
Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,22 @@ The following dependencies are used for this project. Feel free to use other dep

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

### `npm run list:province`

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

### `npm run example`

Expand Down Expand Up @@ -172,17 +176,17 @@ file = new ExcelFile({

// listMunicipalities() lists all municipalities
// for each province
const municipalitiesFromProvince =
file.listMunicipalities(['Albay','Masbate','Sorsogon'])
const provinces = ['Albay','Masbate','Sorsogon']
const municipalitiesFromProvince = file.listMunicipalities(provinces)

// writeMunicipalities() writes municipalities data in a JSON file
// writeMunicipalities() writes municipalities data to a JSON file
file.writeMunicipalities({
provinces: municipalitiesFromProvince,
provinces,
fileName: path.join(__dirname, 'municipalities.json'),
prettify: true
})

// JSON data of the parsed excel file will be accessible on
// JSON data of the parsed excel file will is accessible on
// file.datalist
console.log(file.datalist)
```
Expand Down
2 changes: 1 addition & 1 deletion data/regions.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"name": "National Capital Region",
"abbrev": "NCR",
"region_num": "",
"provinces": ["Metro Manila"]
"provinces": ["Metropolitan Manila"]
},
{
"name": "Region IV-A",
Expand Down
13 changes: 10 additions & 3 deletions src/classes/excel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ class ExcelFile {
/**
* Writes queried municipalities data to a JSON file.
* Lists municipalities by by provinces.
* @param {String} provinces - Array of case-sensitive province names. Starts with an upper case.
* @param {String} filName - Full file path to a JSON file
* @param {String[]} provinces - Array of case-sensitive province names. Starts with an upper case.
* @param {String} fielName - Full file path to a JSON file
* @param {Bool} prettify - Write the JSON content with proper spacings and newlines
* @returns
*/
Expand All @@ -263,12 +263,19 @@ class ExcelFile {
throw new Error('Please enter a filename ending in .json')
}

if (!/\.(json)$/i.test(fileName)) {
throw new Error('Please enter a filename ending in .json')
}

try {
// List the municipalities
const municipalities = this.listMunicipalities({ provinces })

const url = (this.#url) ? this.#url : `local datasource cache from ${process.env.DEFAULT_EXCEL_FILE_URL}`

const str = {
metadata: {
source: process.env.EXCEL_FILE_URL || '',
source: url || '',
title: 'List of PH Municipalities By Province and Region',
description: 'This dataset generated with reference to the excel file contents from the source URL.',
date_created: new Date().toDateString()
Expand Down
2 changes: 1 addition & 1 deletion src/lib/selector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const selectDataSource = async () => {
while (!exit) {
// Prompt to enter the download URL of a remote excel file
if (url === undefined) {
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]:')
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]: ')

if (askDownload === 'Y') {
const pathToFile = path.join(process.cwd(), 'datasource.xlsx')
Expand Down
49 changes: 30 additions & 19 deletions src/scripts/by_province.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ const selectDataSource = require('../lib/selector')
// Lists all municipalities under the specified provinces.
const main = async () => {
let exit = false
let ExcelHandler
let ExcelHandler = null

while (!exit) {
// Prompt to enter the download URL of a remote excel file or use the default local excel file
ExcelHandler = await selectDataSource()
if (ExcelHandler === null) {
ExcelHandler = await selectDataSource()
}

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

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

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

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

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

console.log(`JSON file created in ${filePath}`)
}
try {
ExcelHandler.writeMunicipalities({
provinces,
fileName: filePath
})

const ex = await prompt('\nExit? (Enter X to exit): ')
exit = (ex === 'X')
console.log(`JSON file created in ${filePath}\n`)
} catch (err) {
console.log(err.message)
}

const ex = await prompt('Exit? (Enter X to exit): ')
exit = (ex === 'X')
}
}
}
}
}

Expand Down
31 changes: 20 additions & 11 deletions src/scripts/by_region.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ const regions = require('../../data/regions.json')
// Lists all municipalities under the provinces of a region.
const main = async () => {
let exit = false
let ExcelHandler
let ExcelHandler = null
let idx

while (!exit) {
// Prompt to enter the download URL of a remote excel file or use the default local excel file
ExcelHandler = await selectDataSource()
if (ExcelHandler === null) {
ExcelHandler = await selectDataSource()
}

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

if (region) {
// Check if the region name exists in the masterlist
const idx = regions.data.findIndex(item => item.name === region)
idx = regions.data.findIndex(item => item.name === region)

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

ExcelHandler.writeMunicipalities({
provinces,
fileName: filePath
})
try {
ExcelHandler.writeMunicipalities({
provinces,
fileName: filePath
})

console.log(`JSON file created in ${filePath}`)
console.log(`JSON file created in ${filePath}`)
} catch (err) {
console.log(err.message)
}
}
}
}

const ex = await prompt('\nExit? (Enter X to exit): ')
exit = (ex === 'X')
if (idx >= 0) {
const ex = await prompt('\nExit? (Enter X to exit): ')
exit = (ex === 'X')
}
}
}

Expand Down

0 comments on commit 6bde9d8

Please sign in to comment.