File size: 4,650 Bytes
5c2ed06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
 * Imports and generates the '@smogon/sets' package.
 * Pokemon Showdown - http://pokemonshowdown.com/
 *
 * Run with `node tools/set-import [version]`. If version is not specified,
 * the 'patch' version of the existing package will be bumped. If version is
 * 'minor' or 'monthly', the minor version will be bumped. In general, every
 * month after usage stats are processed this script should be run to update
 * the sets package and bump the minor version. If this script is run in
 * between usage stats updates, the patch version should be bumped. If a
 * breaking change occurs to the output format, the major version must be
 * bumped (with 'major' or 'breaking' as the version argument). The exact
 * version string (eg. '1.2.3') can also be provided. After creating the set
 * import, provided there are no serious errors, the package can be released
 * by running `npm publish` in the `sets/` directory.
 *
 * @license MIT
 */

'use strict';

const child_process = require('child_process');
const path = require('path');
const fs = require('fs');
const shell = cmd => child_process.execSync(cmd, { stdio: 'inherit', cwd: path.resolve(__dirname, '../..') });
shell('node build');

function missing(dep) {
	try {
		require.resolve(dep);
		return false;
	} catch (err) {
		if (err.code !== 'MODULE_NOT_FOUND') throw err;
		return true;
	}
}

// We depend on smogon as a devDependency in order to get the typing information
if (missing('smogon')) shell(`npm install --no-save smogon`);

// Rather obnoxiously, the TeamValidator used by the importer refers to
// rulesets which rely on Chat.plural to be set up, so we do so here.
global.Chat = {};
Chat.plural = function (num, plural = 's', singular = '') {
	if (num && typeof num.length === 'number') {
		num = num.length;
	} else if (num && typeof num.size === 'number') {
		num = num.size;
	} else {
		num = Number(num);
	}
	return (num !== 1 ? plural : singular);
};

const importer = require('../../dist/tools/set-import/importer.js');

const SETS = path.resolve(__dirname, 'sets');
(async () => {
	// Clean up old artifacts
	for (const file of fs.readdirSync(SETS)) {
		if (file.startsWith('gen') && file.endsWith('json')) {
			fs.unlinkSync(path.join(SETS, file));
		}
	}
	const imports = [];
	for (const [i, generationData] of (await importer.importAll()).entries()) {
		fs.writeFileSync(path.resolve(SETS, `gen${i + 1}.json`), JSON.stringify(generationData));
		imports.push(`gen${i + 1}`);
		for (const format in generationData) {
			fs.writeFileSync(path.resolve(SETS, `${format}.json`), JSON.stringify(generationData[format]));
			imports.push(format);
		}
	}

	let version = process.argv[2];
	if (!version || version.match(/^[^\d]/)) {
		try {
			const current = require('./sets/package.json').version;
			const [major, minor, patch] = current.split('.');
			if (version === 'major' || version === 'breaking') {
				version = `${Number(major) + 1}.0.0`;
			} else if (version === 'minor' || version === 'monthly') {
				version = `${major}.${Number(minor) + 1}.0`;
			} else {
				version = `${major}.${minor}.${Number(patch) + 1}`;
			}
		} catch {
			console.error("Version required to create '@smogon/sets' package");
			process.exit(1);
		}
	}

	const packagejson = {
		"name": "@smogon/sets",
		version,
		"description": "Set data imported from Smogon.com and used on Pokémon Showdown",
		"main": "index.js",
		"unpkg": "index.js",
		"types": "index.d.ts",
		"repository": "github:smogon/sets",
		"publishConfig": {
			"access": "public",
		},
		"license": "UNLICENSED", // The code/typings are MIT, but not all sources of data fall under MIT
	};
	fs.writeFileSync(path.resolve(SETS, 'package.json'), JSON.stringify(packagejson, null, 2));

	const indexjs = [
		'"use strict";',
		'var JSON;',
		// This hack allows us to require this package in Node and in the browser
		// and have the Node code which uses `require` get stripped on web.
		'if (typeof window === "undefined") {',
		'	JSON = {',
		imports.map(n => `		"${n}": load("./${n}.json")`).join(',\n'),
		'	};',
		'} else {',
		'	JSON = {',
		imports.map(n => `		"${n}": import("./${n}.json")`).join(',\n'),
		'	};',
		'}',
		'function load(path) {',
		'	return Promise.resolve(require(path));',
		'}',
		'function forGen(gen) {',
		// eslint-disable-next-line no-template-curly-in-string
		'	return JSON[`gen${typeof gen === "number" ? gen : gen.num}`];',
		'}',
		'exports.forGen = forGen;',
		'function forFormat(format) {',
		'	return JSON[format];',
		'}',
		'exports.forFormat = forFormat;',
	].join('\n');
	fs.writeFileSync(path.resolve(SETS, 'index.js'), indexjs);
})().catch(err => console.error(err));