backend v4 half

This commit is contained in:
2025-07-18 09:20:40 +02:00
parent aba7a506ad
commit 725516ad6c
4183 changed files with 217684 additions and 75056 deletions
@@ -0,0 +1,11 @@
// Creates a `package.json` file in the CommonJS `build` folder.
// That marks that whole folder as CommonJS so that Node.js doesn't complain
// about `require()`-ing those files.
import fs from 'fs'
fs.writeFileSync('./build/package.json', JSON.stringify({
name: 'libphonenumber-js/build',
type: 'commonjs',
private: true
}, null, 2), 'utf8')
@@ -0,0 +1,12 @@
import fs from 'fs'
import { download } from 'libphonenumber-metadata-generator'
const url = process.argv[2]
const outputPath = process.argv[3]
download(url).then((contents) => {
fs.writeFileSync(outputPath, contents)
}).catch((error) => {
console.error(error)
process.exit(1)
})
@@ -0,0 +1,13 @@
import metadata from '../metadata.min.json' assert { type: 'json' }
import fs from 'fs'
const countryCodes = Object.keys(metadata.countries)
fs.writeFileSync(
'./types.d.ts',
fs.readFileSync('./types.d.ts', 'utf-8').replace(
/export type CountryCode = .*;/,
`export type CountryCode = ${countryCodes.map(_ => `'${_}'`).join(' | ')};`
),
'utf-8'
)
@@ -0,0 +1,81 @@
import minimist from 'minimist'
import path from 'path'
import fs from 'fs'
import { version, generate, compress } from 'libphonenumber-metadata-generator'
// https://ru.stackoverflow.com/questions/1281148/referenceerror-dirname-is-not-defined
import { fileURLToPath } from 'url'
import { dirname } from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
// const REGION_CODE_FOR_NON_GEO_ENTITY = '001'
const input = fs.readFileSync(path.join(__dirname, process.argv[2]), 'utf8')
const output_file = process.argv[3]
const command_line_arguments = minimist(process.argv.slice(4))
// Included countries
let included_countries
if (command_line_arguments.countries) {
included_countries = command_line_arguments.countries.split(',')
console.log('Included countries:', included_countries)
included_countries = new Set(included_countries)
}
// Include all regular expressions
let extended = false
if (command_line_arguments.extended) {
console.log('Include extra validation regular expressions')
extended = true
}
// Included phone number types
let included_phone_number_types
if (command_line_arguments.types) {
included_phone_number_types = command_line_arguments.types.split(',')
console.log('Included phone number types:', included_phone_number_types)
included_phone_number_types = new Set(included_phone_number_types)
}
// Generate and compress metadata
generate(input, version, included_countries, extended, included_phone_number_types).then((output) => {
// Write uncompressed metadata into a file for easier debugging
if (command_line_arguments.debug) {
console.log('Output uncompressed JSON for debugging')
fs.writeFileSync(path.join(__dirname, '../metadata.json'), JSON.stringify(output, undefined, 3))
}
// Compress the generated metadata
fs.writeFileSync(path.join(__dirname, output_file), JSON.stringify(compress(output)))
// Output mobile phone number type examples
if (command_line_arguments.examples === 'mobile') {
var examples = Object.keys(output.countries).reduce(function(out, country_code) {
// if (country_code === REGION_CODE_FOR_NON_GEO_ENTITY) {
// return out
// }
var mobile = output.countries[country_code].examples.mobile
var fixed_line = output.countries[country_code].examples.fixed_line
if (mobile) {
out[country_code] = mobile
}
// "TA" country doesn't have any mobile phone number example
else if (fixed_line) {
console.warn(`Country ${country_code} doesn't have a mobile phone number example. Substituting with a fixed line phone number example.`)
out[country_code] = fixed_line
} else {
console.error(`Country ${country_code} doesn't have neither a mobile phone number example nor a fixed line phone number example.`)
// `async` errors aren't being caught at the top level in Node.js
process.exit(1)
}
return out
}, {})
fs.writeFileSync(
path.join(__dirname, '../examples.mobile.json'),
JSON.stringify(examples)
)
}
})
@@ -0,0 +1,14 @@
import fs from 'fs'
const COMMENT = '// This file is a workaround for a bug in web browsers\' "native"' + '\n' +
'// ES6 importing system which is uncapable of importing "*.json" files.' + '\n' +
'// https://github.com/catamphetamine/libphonenumber-js/issues/239'
const path = process.argv[2]
jsonToJs(path)
function jsonToJs(path) {
let contents = fs.readFileSync(path, 'utf-8')
contents = COMMENT + '\n' + 'export default ' + contents
fs.writeFileSync(path + '.js', contents)
}
@@ -0,0 +1,26 @@
import exec from './modules/exec.js'
let metadata_branch_exists = false
try
{
exec('git rev-parse --verify update-metadata')
metadata_branch_exists = true
}
catch (error)
{
if (error.message.indexOf('fatal: Needed a single revision') === -1)
{
throw error
}
}
if (metadata_branch_exists)
{
console.log(exec('git checkout master'))
console.log(exec('git branch -D update-metadata'))
}
console.log(exec('git pull'))
console.log(exec('git branch update-metadata origin/master'))
console.log(exec('git checkout update-metadata'))
@@ -0,0 +1,47 @@
// In order for this script to work:
//
// * Install `hub` command line tool: `brew install hub`
// * Create a "Personal Access Token" in GitHub account settings (just "repo_public" would be enough)
// * Tell `hub` to use the token for creating GitHub pull requests: `echo "---\ngithub.com:\n- protocol: https\n user: GITHUB_USERNAME\n oauth_token: TOKEN" >> ~/.config/hub`
import update_metadata from './modules/update-metadata.js'
import commit from './modules/commit.js'
import exec from './modules/exec.js'
if (update_metadata())
{
commit()
console.log()
console.log('========================================')
console.log('= Pushing changes =')
console.log('========================================')
console.log()
// Delete previous `update-metadata` remote branch
// (if it already exists)
if (exec('git ls-remote --heads origin update-metadata'))
{
console.log(exec('git push origin update-metadata --delete'))
}
// Push the local `update-metadata` branch to GitHub
console.log(exec('git push origin update-metadata'))
console.log()
console.log('========================================')
console.log('= Pushed. Creating Pull Request. =')
console.log('========================================')
console.log()
console.log(exec('hub pull-request -m "Updated metadata" -b catamphetamine/libphonenumber-js:master -h update-metadata'))
console.log()
console.log('========================================')
console.log('= Pull Request created =')
console.log('========================================')
console.log()
console.log(exec('git checkout master'))
console.log(exec('git branch -D update-metadata'))
}
@@ -0,0 +1,30 @@
import update_metadata from './modules/update-metadata.js'
import commit from './modules/commit.js'
import exec from './modules/exec.js'
if (update_metadata())
{
commit()
console.log()
console.log('========================================')
console.log('= Pushing changes =')
console.log('========================================')
console.log()
// Delete previous `update-metadata` remote branch
// (if it already exists)
if (exec('git ls-remote --heads origin update-metadata'))
{
console.log(exec('git push origin update-metadata --delete'))
}
// Push the local `update-metadata` branch to GitHub
console.log(exec('git push origin update-metadata'))
console.log()
console.log('==========================================')
console.log('= Pushed. Create Pull Request on GitHub. =')
console.log('==========================================')
console.log()
}
@@ -0,0 +1,32 @@
import update_metadata from './modules/update-metadata.js'
import commit from './modules/commit.js'
import exec from './modules/exec.js'
if (update_metadata())
{
commit()
console.log()
console.log('========================================')
console.log('= Pushing changes =')
console.log('========================================')
console.log()
console.log(exec('git push'))
console.log()
console.log('========================================')
console.log('= Pushed. Releasing. =')
console.log('========================================')
console.log()
console.log(exec('npm version patch'))
console.log(exec('npm publish'))
console.log(exec('git push'))
console.log()
console.log('========================================')
console.log('= Released =')
console.log('========================================')
console.log()
}
@@ -0,0 +1,14 @@
import exec from './exec.js'
export default function()
{
console.log()
console.log('========================================')
console.log('= Committing changes =')
console.log('========================================')
console.log()
console.log(exec('git add .'))
console.log(exec('git commit -m "Updated metadata"'))
}
@@ -0,0 +1,6 @@
import child_process from 'child_process'
export default function exec(command)
{
return child_process.execSync(command).toString().trim()
}
@@ -0,0 +1,88 @@
import exec from './exec.js'
export default function()
{
var metadata_changed = exec('git ls-files --modified PhoneNumberMetadata.xml')
if (!metadata_changed)
{
console.log()
console.log('========================================')
console.log('= Metadata is up-to-date. Exiting. =')
console.log('========================================')
console.log()
return
}
console.log()
console.log('========================================')
console.log('= Metadata has changed, updating files =')
console.log('========================================')
console.log()
console.log(exec('npm run metadata:generate'))
console.log()
console.log('========================================')
console.log('= Running tests =')
console.log('========================================')
console.log()
// console.log('* Actually not running tests because if they fail then it won\'t be reported in any way, and if instead tests fail for the Pull Request on github then the repo owner will be notified by Travis CI about that.')
console.log(exec('npm run build'))
console.log(exec('npm test'))
var modified_files = exec('git ls-files --modified').split(/\s/)
var unexpected_modified_files = modified_files.filter(function(file)
{
return file !== 'PhoneNumberMetadata.xml' &&
!/^metadata\.[a-z]+\.json$/.test(file) &&
!/^examples\.[a-z]+\.json$/.test(file)
})
// Turned off this "modified files" check
// because on Windows random files constantly got "modified"
// without actually being modified.
// (perhaps something related to line endings)
if (false && unexpected_modified_files.length > 0)
{
var error
error += 'Only `PhoneNumberMetadata.xml`, `metadata.*.json` and `examples.*.json` files should be modified. Unexpected modified files:'
error += '\n'
error += '\n'
error += unexpected_modified_files.join('\n')
console.log()
console.log('========================================')
console.log('= Error =')
console.log('========================================')
console.log()
console.log(error)
throw new Error(error)
}
// Doesn't work
//
// // http://stackoverflow.com/questions/33610682/git-list-of-staged-files
// var staged_files = exec('git diff --name-only --cached').split(/\s/)
//
// if (staged_files.length > 0)
// {
// console.log()
// console.log('========================================')
// console.log('= Error =')
// console.log('========================================')
// console.log()
// console.log('There are some staged files already. Aborting metadata update process.')
// console.log()
// console.log(staged_files.join('\n'))
//
// process.exit(1)
// }
return true
}