Skip to content

Commit

Permalink
Add 'SONGS_TO_BE_DELETED_FILE_NAME' constant and upload the file to G…
Browse files Browse the repository at this point in the history
…oogle Drive.
  • Loading branch information
ioanlucut committed Sep 25, 2023
1 parent da478b0 commit 5925b7e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ export const EMPTY_SPACE = ' ';
export const PRO_EXTENSION = `.pro`;

export const MANIFEST_FILE_NAME = 'manifest.json';
export const SONGS_TO_BE_DELETED_FILE_NAME = 'songsToBeDeleted.json';

export const DS_STORE_FILE = '.DS_Store';
6 changes: 6 additions & 0 deletions src/converterService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
DS_STORE_FILE,
MANIFEST_FILE_NAME,
PRO_EXTENSION,
SONGS_TO_BE_DELETED_FILE_NAME,
TXT_EXTENSION,
} from './constants';
import {
Expand Down Expand Up @@ -151,6 +152,10 @@ export const getBasicDeploymentInfo = async (
deploymentVersionedDir,
MANIFEST_FILE_NAME,
);
const localObsoleteSongsFilePath = path.join(
deploymentVersionedDir,
SONGS_TO_BE_DELETED_FILE_NAME,
);

return {
versionedDir,
Expand All @@ -159,5 +164,6 @@ export const getBasicDeploymentInfo = async (
deployableSongs,
currentManifest,
localManifestFilePath,
localObsoleteSongsFilePath,
};
};
43 changes: 26 additions & 17 deletions src/gDriveConverterRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import {
getExistingFoldersFromRoot,
getPreviousManifestFileBy,
uploadSongsAndManifestToGDrive,
uploadAssetsToGDrive,
} from './gDriveService';

const getPreviousRemoteInventoryManifest = async (
Expand Down Expand Up @@ -74,6 +74,7 @@ export const convertSongsToPP7FormatRemotely = async ({
deployableSongs,
currentManifest,
localManifestFilePath,
localObsoleteSongsFilePath,
} = await getBasicDeploymentInfo(sourceDir, baseLocalDir);

assert.ok(
Expand All @@ -85,6 +86,8 @@ export const convertSongsToPP7FormatRemotely = async ({
// Create directory
fsExtra.ensureDirSync(deploymentVersionedDir);

// ---
// Write manifest
fs.writeFileSync(localManifestFilePath, JSON.stringify(currentManifest));

// ---
Expand All @@ -103,7 +106,7 @@ export const convertSongsToPP7FormatRemotely = async ({
`[Remote]: No previous deployment found in. Skip incremental deployments by doing a full deployment. Please proceed with applying the theme.`,
);

await uploadSongsAndManifestToGDrive(
await uploadAssetsToGDrive(
getConvertedAndWrittenToLocalOutDirSongs(
deployableSongs,
deploymentVersionedDir,
Expand All @@ -116,12 +119,12 @@ export const convertSongsToPP7FormatRemotely = async ({
return;
}

if (process.env.FORCE_RELEASE_OF_ALL_SONGS === 'true') {
if (isEqual(process.env.FORCE_RELEASE_OF_ALL_SONGS, 'true')) {
console.log(
`[Remote]: Force release of all songs from GH. Skip incremental deployments by doing a full deployment. Please proceed with applying the theme.`,
);

await uploadSongsAndManifestToGDrive(
await uploadAssetsToGDrive(
getConvertedAndWrittenToLocalOutDirSongs(
deployableSongs,
deploymentVersionedDir,
Expand All @@ -144,13 +147,28 @@ export const convertSongsToPP7FormatRemotely = async ({
previousManifest,
);

if (isEmpty(newOrUpdatedSongs)) {
if (isEmpty(newOrUpdatedSongs) && isEmpty(toBeRemovedFileNames)) {
console.log(
'[Remote]: Skip incremental local deployments as no changes have been found between the last two versions.',
`[Remote]: Skip incremental local deployments as no changes have been found between the last two versions.`,
);

return;
}

// ---
// Write empty removal file
fs.writeFileSync(
localObsoleteSongsFilePath,
JSON.stringify(toBeRemovedFileNames),
);

// ---
// Write some stats
// ---
console.log(chalk.green(JSON.stringify(versionedDir)));
console.log(chalk.green(JSON.stringify(newOrUpdatedSongs)));
console.log(chalk.red(JSON.stringify(toBeRemovedFileNames)));

const partialDeployableSongs = deployableSongs.filter(({ song: { id } }) =>
newOrUpdatedSongs
.map(({ id: newOrUpdatedSongId }) => newOrUpdatedSongId)
Expand All @@ -164,19 +182,10 @@ export const convertSongsToPP7FormatRemotely = async ({
config,
);

await uploadSongsAndManifestToGDrive(
await uploadAssetsToGDrive(
convertedAndWrittenToLocalOutDirSongs,
versionedDir,
localManifestFilePath,
localObsoleteSongsFilePath,
);

console.log(chalk.green(JSON.stringify(versionedDir)));
console.log(chalk.green(JSON.stringify(newOrUpdatedSongs)));
console.log(chalk.red(JSON.stringify(toBeRemovedFileNames)));

if (!isEmpty(toBeRemovedFileNames)) {
console.log(
`[Remote]: The following songs have been removed: ${toBeRemovedFileNames} manually.`,
);
}
};
24 changes: 22 additions & 2 deletions src/gDriveService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { logProcessingFile } from './core';
import { ConvertedFileStats, SongsInventoryManifest } from './types';
import assert from 'node:assert';
import { first, size } from 'lodash';
import { MANIFEST_FILE_NAME, SONGS_TO_BE_DELETED_FILE_NAME } from './constants';

const MANIFEST_FILE_NAME = 'manifest.json';
const FOLDER_MIME_TYPE = 'application/vnd.google-apps.folder';
const DEFAULT_PP7_MIME_FILE = 'application/octet-stream';
const JSON_MIME_TYPE = 'application/json';
Expand Down Expand Up @@ -37,10 +37,11 @@ const getGoogleDriveClient: () => Promise<drive_v3.Drive> = async () => {
return gDriveClient;
};

export const uploadSongsAndManifestToGDrive = async (
export const uploadAssetsToGDrive = async (
convertedFilesStats: ConvertedFileStats[],
versionedDir: string,
localManifestFilePath: string,
localObsoleteSongsFilePath?: string,
) => {
// ---
// New GDrive folder
Expand Down Expand Up @@ -74,6 +75,25 @@ export const uploadSongsAndManifestToGDrive = async (
},
});

if (localObsoleteSongsFilePath) {
// ---
// Upload obsolete songs list

await (
await getGoogleDriveClient()
).files.create({
requestBody: {
name: SONGS_TO_BE_DELETED_FILE_NAME,
mimeType: JSON_MIME_TYPE,
parents: [newGDriveFolder.data.id as string],
},
media: {
mimeType: JSON_MIME_TYPE,
body: fs.createReadStream(localObsoleteSongsFilePath),
},
});
}

(
await pMap(
convertedFilesStats,
Expand Down

0 comments on commit 5925b7e

Please sign in to comment.