mx-jpush-expo
Version:
Expo 集成极光推送(JPush)一体化解决方案,支持 iOS/Android 厂商通道
207 lines • 7.73 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeContents = mergeContents;
exports.mergeContentsAtLine = mergeContentsAtLine;
exports.mergeContentsAtEnd = mergeContentsAtEnd;
exports.syncGeneratedContents = syncGeneratedContents;
exports.syncGeneratedContentsAtLine = syncGeneratedContentsAtLine;
exports.syncGeneratedContentsAtEnd = syncGeneratedContentsAtEnd;
exports.replaceGeneratedContentsAtLine = replaceGeneratedContentsAtLine;
exports.removeContents = removeContents;
exports.removeGeneratedContents = removeGeneratedContents;
exports.createGeneratedHeaderComment = createGeneratedHeaderComment;
exports.createHash = createHash;
/**
* Get line indexes for the generated section of a file.
*
* @param src
*/
const crypto_1 = __importDefault(require("crypto"));
function getGeneratedSectionIndexes(src, tag) {
const contents = src.split('\n');
const start = contents.findIndex((line) => new RegExp(`@generated begin ${tag} -`).test(line));
const end = contents.findIndex((line) => new RegExp(`@generated end ${tag}$`).test(line));
return { contents, start, end };
}
/**
* Merge the contents of two files together and add a generated header.
*
* @param src contents of the original file
* @param newSrc new contents to merge into the original file
* @param identifier used to update and remove merges
* @param anchor regex to where the merge should begin
* @param offset line offset to start merging at (<1 for behind the anchor)
* @param comment comment style `//` or `#`
*/
function mergeContents({ src, newSrc, tag, anchor, offset = 0, comment = "//", }) {
const header = createGeneratedHeaderComment(newSrc, tag, comment);
if (!src.includes(header)) {
// Ensure the old generated contents are removed.
const sanitizedTarget = removeGeneratedContents(src, tag);
return {
contents: addLines(sanitizedTarget ?? src, anchor, offset, [
header,
...newSrc.split('\n'),
`${comment} @generated end ${tag}`,
]),
didMerge: true,
didClear: !!sanitizedTarget,
};
}
return { contents: src, didClear: false, didMerge: false };
}
function mergeContentsAtLine({ src, newSrc, tag, lineIndex, offset = 0, comment = '//', }) {
const header = createGeneratedHeaderComment(newSrc, tag, comment);
if (!src.includes(header)) {
const sanitizedTarget = removeGeneratedContents(src, tag);
return {
contents: addLinesAtIndex(sanitizedTarget ?? src, lineIndex, offset, [
header,
...newSrc.split('\n'),
`${comment} @generated end ${tag}`,
]),
didMerge: true,
didClear: !!sanitizedTarget,
};
}
return { contents: src, didClear: false, didMerge: false };
}
function mergeContentsAtEnd({ src, newSrc, tag, comment = '//', }) {
const header = createGeneratedHeaderComment(newSrc, tag, comment);
if (src.includes(header)) {
return { contents: src, didClear: false, didMerge: false };
}
const sanitizedTarget = removeGeneratedContents(src, tag);
const target = sanitizedTarget ?? src;
const normalizedTarget = target.endsWith('\n') ? target : `${target}\n`;
const needsSpacer = normalizedTarget.trim().length > 0 && !normalizedTarget.endsWith('\n\n');
const contents = `${normalizedTarget}${needsSpacer ? '\n' : ''}${[
header,
...newSrc.split('\n'),
`${comment} @generated end ${tag}`,
].join('\n')}\n`;
return {
contents,
didMerge: true,
didClear: !!sanitizedTarget,
};
}
function syncGeneratedContents({ src, newSrc, tag, anchor, offset = 0, comment = '//', }) {
if (!newSrc.trim()) {
return removeContents({ src, tag });
}
return mergeContents({
src,
newSrc,
tag,
anchor,
offset,
comment,
});
}
function syncGeneratedContentsAtLine({ src, newSrc, tag, lineIndex, offset = 0, comment = '//', }) {
if (!newSrc.trim()) {
return removeContents({ src, tag });
}
return mergeContentsAtLine({
src,
newSrc,
tag,
lineIndex,
offset,
comment,
});
}
function syncGeneratedContentsAtEnd({ src, newSrc, tag, comment = '//', }) {
if (!newSrc.trim()) {
return removeContents({ src, tag });
}
return mergeContentsAtEnd({
src,
newSrc,
tag,
comment,
});
}
function replaceGeneratedContentsAtLine({ src, newSrc, tag, getLineIndex, offset = 0, comment = '//', }) {
if (!newSrc.trim()) {
return removeContents({ src, tag });
}
const sanitizedTarget = removeGeneratedContents(src, tag) ?? src;
const lineIndex = getLineIndex(sanitizedTarget);
return mergeContentsAtLine({
src: sanitizedTarget,
newSrc,
tag,
lineIndex,
offset,
comment,
});
}
function removeContents({ src, tag }) {
// Ensure the old generated contents are removed.
const sanitizedTarget = removeGeneratedContents(src, tag);
return {
contents: sanitizedTarget ?? src,
didMerge: false,
didClear: !!sanitizedTarget,
};
}
function addLines(content, find, offset, toAdd) {
const lines = content.split('\n');
let lineIndex = lines.findIndex((line) => line.match(find));
if (lineIndex < 0) {
const error = new Error(`Failed to match "${find}" in contents:\n${content}`);
error.code = 'ERR_NO_MATCH';
throw error;
}
return addLinesAtIndex(content, lineIndex, offset, toAdd);
}
function addLinesAtIndex(content, lineIndex, offset, toAdd) {
const lines = content.split('\n');
if (lineIndex < 0 || lineIndex >= lines.length) {
throw new Error(`Failed to insert contents at invalid line index: ${lineIndex}`);
}
let insertionIndex = lineIndex + offset;
if (insertionIndex < 0) {
insertionIndex = 0;
}
else if (insertionIndex > lines.length) {
insertionIndex = lines.length;
}
for (const newLine of toAdd) {
lines.splice(insertionIndex, 0, newLine);
insertionIndex += 1;
}
return lines.join('\n');
}
/**
* Removes the generated section from a file, returns null when nothing can be removed.
* This sways heavily towards not removing lines unless it's certain that modifications were not made manually.
*
* @param src
*/
function removeGeneratedContents(src, tag) {
const { contents, start, end } = getGeneratedSectionIndexes(src, tag);
if (start > -1 && end > -1 && start < end) {
contents.splice(start, end - start + 1);
// TODO: We could in theory check that the contents we're removing match the hash used in the header,
// this would ensure that we don't accidentally remove lines that someone added or removed from the generated section.
return contents.join('\n');
}
return null;
}
function createGeneratedHeaderComment(contents, tag, comment) {
const hashKey = createHash(contents);
// Everything after the `${tag} ` is unversioned and can be freely modified without breaking changes.
return `${comment} @generated begin ${tag} - expo prebuild (DO NOT MODIFY) ${hashKey}`;
}
function createHash(src) {
// this doesn't need to be secure, the shorter the better.
const hash = crypto_1.default.createHash('sha1').update(src).digest('hex');
return `sync-${hash}`;
}
//# sourceMappingURL=generateCode.js.map