-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
112 lines (99 loc) · 2.75 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/**
* @file
* Sync ESlint config from latest Drupal core release.
*/
/* eslint import/no-extraneous-dependencies: ["error", {"devDependencies": ["build.js"]}] */
/* eslint no-console: "off" */
const fs = require('fs/promises');
const https = require('https');
const { parseStringPromise } = require('xml2js');
const selfPackage = require('./package.json');
/**
* Gets string data from a URL.
*
* @param {string} url The URL to GET.
* @return {Promise<string>} Response body.
*/
const get = (url) =>
new Promise((resolve, reject) => {
const options = {
headers: {
'User-Agent': '@projectcosmic/eslint-config-drupal',
},
};
https.get(url, options, (response) => {
const { statusCode } = response;
// Any 2xx status code signals a successful response but here we're only
// checking for 200.
if (statusCode !== 200) {
reject(
new Error(`Request Failed for ${url}. Status Code: ${statusCode}`),
);
// Consume response data to free up memory.
response.resume();
return;
}
response.setEncoding('utf8');
let rawData = '';
response.on('error', reject);
response.on('data', (chunk) => {
rawData += chunk;
});
response.on('end', () => {
try {
resolve(rawData);
} catch (error) {
reject(error);
}
});
});
});
/**
* Updates the eslint config.
*
* @param {string} tag The tag name to use.
*/
const setESlintConfig = async (tag) =>
fs.writeFile(
'.eslintrc.json',
await get(
`https://git.drupalcode.org/project/drupal/-/raw/${tag}/core/.eslintrc.json`,
),
);
/**
* Updates the eslint dependencies.
*
* @param {string} tag The tag name to use.
*/
const setESLintDependencies = async (tag) => {
const drupalPackage = JSON.parse(
await get(
`https://git.drupalcode.org/project/drupal/-/raw/${tag}/core/package.json`,
),
);
selfPackage.peerDependencies = Object.fromEntries(
Object.entries(drupalPackage.devDependencies).filter(([packageName]) =>
/^eslint(-.+)?/.test(packageName),
),
);
fs.writeFile(
'package.json',
`${JSON.stringify(selfPackage, undefined, 2)}\n`,
);
};
get('https://updates.drupal.org/release-history/drupal/current')
.then(parseStringPromise)
.then((data) =>
data.project.releases[0].release
.filter(({ tag: [tag] }) => /^(\d+\.){2}\d+$/.test(tag))
.slice(0, 1)
.forEach(async ({ tag: [tag] }) => {
setESlintConfig(tag);
setESLintDependencies(tag);
const prefix = process.argv.slice(2).includes('--plain')
? ''
: 'Sync for Drupal ';
console.log(`${prefix}${tag}`);
}),
)
.catch((error) => console.error(error));