diff --git a/src/constants.ts b/src/constants.ts index 137e553..6fb6d24 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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'; diff --git a/src/converterService.ts b/src/converterService.ts index c0fb8b8..740f971 100644 --- a/src/converterService.ts +++ b/src/converterService.ts @@ -11,6 +11,7 @@ import { DS_STORE_FILE, MANIFEST_FILE_NAME, PRO_EXTENSION, + SONGS_TO_BE_DELETED_FILE_NAME, TXT_EXTENSION, } from './constants'; import { @@ -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, @@ -159,5 +164,6 @@ export const getBasicDeploymentInfo = async ( deployableSongs, currentManifest, localManifestFilePath, + localObsoleteSongsFilePath, }; }; diff --git a/src/gDriveConverterRunner.ts b/src/gDriveConverterRunner.ts index 354fdf3..88c6e8a 100644 --- a/src/gDriveConverterRunner.ts +++ b/src/gDriveConverterRunner.ts @@ -14,7 +14,7 @@ import { import { getExistingFoldersFromRoot, getPreviousManifestFileBy, - uploadSongsAndManifestToGDrive, + uploadAssetsToGDrive, } from './gDriveService'; const getPreviousRemoteInventoryManifest = async ( @@ -74,6 +74,7 @@ export const convertSongsToPP7FormatRemotely = async ({ deployableSongs, currentManifest, localManifestFilePath, + localObsoleteSongsFilePath, } = await getBasicDeploymentInfo(sourceDir, baseLocalDir); assert.ok( @@ -85,6 +86,8 @@ export const convertSongsToPP7FormatRemotely = async ({ // Create directory fsExtra.ensureDirSync(deploymentVersionedDir); + // --- + // Write manifest fs.writeFileSync(localManifestFilePath, JSON.stringify(currentManifest)); // --- @@ -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, @@ -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, @@ -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) @@ -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.`, - ); - } }; diff --git a/src/gDriveService.ts b/src/gDriveService.ts index 5001299..59f8f1a 100644 --- a/src/gDriveService.ts +++ b/src/gDriveService.ts @@ -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'; @@ -37,10 +37,11 @@ const getGoogleDriveClient: () => Promise = async () => { return gDriveClient; }; -export const uploadSongsAndManifestToGDrive = async ( +export const uploadAssetsToGDrive = async ( convertedFilesStats: ConvertedFileStats[], versionedDir: string, localManifestFilePath: string, + localObsoleteSongsFilePath?: string, ) => { // --- // New GDrive folder @@ -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,