-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbuild.js
91 lines (87 loc) · 2.85 KB
/
build.js
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
/**
* Copyright (C) 2022-2024 Permanent Data Solutions, Inc. All Rights Reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const fs = require('fs');
const path = require('path');
const { build } = require('esbuild');
const replace = require('replace-in-file');
const Ajv = require('ajv');
const standaloneCode = require('ajv/dist/standalone').default;
const contracts = ['contract.ts'];
const {
balanceSchema,
removeControllerSchema,
setControllerSchema,
setNameSchema,
setTickerSchema,
setRecordSchema,
removeRecordSchema,
transferSchema,
evolveSchema,
} = require('./schemas');
// build our validation source code
const ajv = new Ajv({
schemas: [
balanceSchema,
removeControllerSchema,
setControllerSchema,
setNameSchema,
setTickerSchema,
setRecordSchema,
removeRecordSchema,
transferSchema,
evolveSchema,
],
code: { source: true, esm: true },
});
const moduleCode = standaloneCode(ajv, {
validateSetRecord: '#/definitions/setRecord',
validateRemoveRecord: '#/definitions/removeRecord',
validateSetController: '#/definitions/setController',
validateRemoveController: '#/definitions/removeController',
validateSetName: '#/definitions/setName',
validateSetTicker: '#/definitions/setTicker',
validateBalance: '#/definitions/balance',
validateTransferTokens: '#/definitions/transfer',
validateEvolve: '#/definitions/evolve',
});
// Now you can write the module code to file
fs.writeFileSync(path.join(__dirname, '/src/validations.js'), moduleCode);
build({
entryPoints: contracts.map((source) => {
return `./src/${source}`;
}),
outdir: './dist',
minify: false,
bundle: true,
format: 'iife',
packages: 'external',
tsconfig: './tsconfig.json',
})
.catch(() => process.exit(1))
// note: SmartWeave SDK currently does not support files in IIFE bundle format, so we need to remove the "iife" part ;-)
// update: it does since 0.4.31, but because viewblock.io is still incompatibile with this version, leaving as is for now.
.finally(() => {
const files = contracts.map((source) => {
return `./dist${source}`.replace('.ts', '.js');
});
replace.sync({
files: files,
from: [/\(\(\) => {/g, /}\)\(\);/g],
to: '',
countMatches: true,
});
});