Skip to content

Commit

Permalink
chore: update scripts (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaywcjlove committed Mar 22, 2022
1 parent bfda686 commit 0fcb5dd
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/core/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
.Trashes
ehthumbs.db
Thumbs.db
README.md

# IDE Specific
nbproject
Expand All @@ -31,3 +30,4 @@ tmp
.changelog
.nyc_output/
.eslintcache
~*.*
32 changes: 17 additions & 15 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
"description": "An util to query china province, city and district data. 中华人民共和国国家标准 GB/T 2260 行政区划代码。",
"main": "index.js",
"scripts": {
"start": "npm run get && npm run get:town && npm run create:data:json:csv && npm run compress",
"get:town": "node script/getTown.js",

"get": "npm run create:province:city && npm run create:level",
"create:province:city": "node script/getProvinceCity.js",
"create:level": "node script/createLevelData.js",

"create:data:json:csv": "node script/createDataCSVAndJSON.js",
"compress": "node script/compress.js"
"①": "------------获取<省市区>数据---------------------------",
"get": "node script/province.js",
"②": "------------检查<区>数据------------------------------",
"check": "node script/check.js",
"③": "------------获取<街道>数据-----------------------------",
"get:town": "node script/town.js",
"④": "------------生成<SQL><省市区(data.json)>数据-----------",
"sql": "node script/sql.js",
"⑤": "------------重新生成新的<层级>数据----------------------",
"level": "node script/level.js",
"⑥": "------------数据拷贝到各个<包>中------------------------",
"copy": "node script/copy.js"
},
"keywords": [
"province",
Expand Down Expand Up @@ -51,16 +54,15 @@
"type": "git",
"url": "https://github.com/uiwjs/province-city-china.git"
},
"author": "",
"license": "MIT",
"dependencies": {
"@province-city-china/types": "8.1.0"
},
"devDependencies": {
"cheerio": "1.0.0-rc.10",
"csvtojson": "2.0.10",
"fs-extra": "9.1.0",
"superagent": "5.3.1",
"superagent-charset": "1.2.0"
"cheerio": "~1.0.0-rc.10",
"csvtojson": "~2.0.10",
"fs-extra": "~10.0.1",
"superagent": "~7.1.1",
"superagent-charset": "~1.2.0"
}
}
50 changes: 50 additions & 0 deletions packages/core/script/level.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const path = require('path');
const FS = require('fs-extra');
const city = require('../dist/city.json')
const area = require('../dist/area.json')
const province = require('../dist/province.json')

;(async () => {
try {
// =================================================
// --------存储一个体积大的层级数据----------->
// =================================================
console.log(`✅ >\x1b[35;1m <省市区>\x1b[0m层级数据:`);
const dataPath = path.resolve(process.cwd(), 'dist/level.json');
const data = province.map(p => {
let cData = [...city].filter(m => m.province === p.province && /00$/.test(m.code)).map(m => {
m.children = area.filter(a => a.city === m.city && a.province === m.province);
return m
});
if (/^(11|31|12|50)$/.test(p.province) && cData.length === 0) {
cData = area.filter(c => c.province === p.province)
}
p.children = cData;
return p;
});
await FS.outputFile(dataPath, JSON.stringify(data, null, 2));
console.log(' \x1b[32;1m✔\x1b[0m 数据保存:', path.relative(process.cwd(), dataPath));

const minDataPath = path.resolve(process.cwd(), 'dist/level.min.json');
const minData = province.map(p => {
let cData = [...city].filter(m => m.province === p.province && /00$/.test(m.code)).map(m => {
m.children = area.filter(a => a.city === m.city && a.province === m.province).map((child) => ({
c: child.code, n: child.name, p: child.province, d: child.city, a: child.area
}));
return { c: m.code, n: m.name, p: m.province, d: m.children };
});
if (/^(11|31|12|50)$/.test(p.province) && cData.length === 0) {
cData = area.filter(c => c.province === p.province).map((child) => ({
c: child.code, n: child.name, p: child.province, d: child.city, a: child.area
}));
}
p.children = cData;
return { c: p.code, n: p.name, p: p.province, d: p.children };
});
await FS.outputFile(minDataPath, JSON.stringify(minData));
console.log(' \x1b[32;1m✔\x1b[0m 数据保存:', path.relative(process.cwd(), minDataPath));
} catch (error) {
console.log(error)
console.log(`\x1b[31;1m ERR:验证<层级数据>数据[]\x1b[0m: ${error.massage}`);
}
})();
67 changes: 38 additions & 29 deletions packages/core/script/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,31 @@
*/
const fs = require('fs-extra');

const sql = `# ************************************************************
# 中华人民共和国行政区划代码 SQL dump
# Generation Time: ${new Date()}
# ************************************************************
DROP TABLE IF EXISTS \`province\`;
CREATE TABLE \`province\` (
\`id\` int(11) unsigned NOT NULL AUTO_INCREMENT,
\`code\` bigint(12) DEFAULT NULL COMMENT '行政区划代码',
\`name\` varchar(32) DEFAULT NULL COMMENT '名称',
\`province\` varchar(32) DEFAULT NULL COMMENT '省/直辖市',
\`city\` varchar(32) DEFAULT NULL COMMENT '市',
\`area\` varchar(32) DEFAULT NULL COMMENT '区',
\`town\` varchar(32) DEFAULT NULL COMMENT '城镇地区',
PRIMARY KEY (\`id\`)
) ENGINE=InnoDB;
LOCK TABLES \`province\` WRITE;
INSERT INTO \`province\` (\`id\`, \`code\`, \`name\`, \`province\`, \`city\`, \`area\`, \`town\`)
VALUES
{{sqldata}};
UNLOCK TABLES;
`;
const sql = `# ************************************************************
# 中华人民共和国行政区划代码 SQL dump
# Generation Time: ${new Date()}
# ************************************************************
DROP TABLE IF EXISTS \`province\`;
CREATE TABLE \`province\` (
\`id\` int(11) unsigned NOT NULL AUTO_INCREMENT,
\`code\` bigint(12) DEFAULT NULL COMMENT '行政区划代码',
\`name\` varchar(32) DEFAULT NULL COMMENT '名称',
\`province\` varchar(32) DEFAULT NULL COMMENT '省/直辖市',
\`city\` varchar(32) DEFAULT NULL COMMENT '市',
\`area\` varchar(32) DEFAULT NULL COMMENT '区',
\`town\` varchar(32) DEFAULT NULL COMMENT '城镇地区',
PRIMARY KEY (\`id\`)
) ENGINE=InnoDB;
LOCK TABLES \`province\` WRITE;
INSERT INTO \`province\` (\`id\`, \`code\`, \`name\`, \`province\`, \`city\`, \`area\`, \`town\`)
VALUES
{{sqldata}};
UNLOCK TABLES;
`;

/**
* 输出 SQL 文件,此为所有数据的输出
Expand All @@ -51,14 +51,23 @@ const fs = require('fs-extra');
const town = require('../dist/town.json');

const data = [...province, ...city, ...area, ...town].map(item => {
return { ...item, city: item.code.substring(2,4) || 0, area: item.code.substring(4,6) || 0, town: item.code.substring(6) || 0 }
return { ...item, city: item.city || 0, area: item.area || 0, town: item.town || 0 }
});

const mindata = [...province, ...city, ...area, ...town].map(item => {
return { c: item.code, n: item.name, p: item.province, y: item.city || 0, a: item.area || 0, t: item.town || 0 }
});

console.log(`✅ >\x1b[35;1m <省市区>\x1b[0m数据:`);
await fs.outputFile('./dist/data.json', JSON.stringify(data, null, 2));
console.log(' \x1b[32;1m✔\x1b[0m 保存与 <town.json> 合并的数据: ./dist/data.json');

await fs.outputFile('./dist/data.min.json', JSON.stringify(mindata));
console.log(' \x1b[32;1m✔\x1b[0m 保存与 <town.json> 合并的数据: ./dist/data.min.json');

const sqlStr = outPutSQL(data);
await fs.outputFile('./dist/data.sql', sqlStr);
console.log(' \x1b[32;1m✔\x1b[0m 数据保存: ./dist/data.sql');
console.log(`✅ >\x1b[35;1m <SQL>\x1b[0m数据:`);
const sqlStr = outPutSQL(data);
await fs.outputFile('./dist/data.sql', sqlStr);
console.log(' \x1b[32;1m✔\x1b[0m 数据保存: ./dist/data.sql');

})();

0 comments on commit 0fcb5dd

Please sign in to comment.